From c1e0ccc87936fdcbf15c735da1c91536f6db82f7 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Mon, 20 Jan 2025 22:35:33 +0000 Subject: [PATCH] test(xml): add unit tests for XML parser (#5) --- bun.lockb | Bin 3044 -> 4560 bytes bunfig.toml | 2 + package.json | 7 ++ src/xmlParser.spec.js | 209 ++++++++++++++++++++++++++++++++++++++++++ test/happydom.js | 6 ++ 5 files changed, 224 insertions(+) create mode 100644 bunfig.toml create mode 100644 src/xmlParser.spec.js create mode 100644 test/happydom.js diff --git a/bun.lockb b/bun.lockb index b9bc9ffeec1b3fa10567c57eda99e946d154e07a..b8905f3f656b6ab29ac697aada1b556077087bb7 100755 GIT binary patch delta 1497 zcmaDNenEMHp5|m>y9r5-)4yv-2+6E@yKM1F;hT42*ra8i-RCOgKGHkEL689qxF?3o z$H#L+I1CJhDLHys#S9F|MajCxMhpzHKnX4ehK6}SniELB2GT&q4F-%13_L*k5mX#V zfz&o|GC|bE18IIB-xo*=P26g&@Tprvo$;@?W`clTY2azAOK%o0F_`Q7XYmrPd)=j; znqCt>crcnwwq&$t%$QsWB$rR#2_(Nw{s|;)CR;MuGuBS7WHO&@z{tV75omb?&~O2u zrEC*FYJ&xZfHqm933BEE1>Jyx%o9KAgH&;D0Sfp41=uHkG_Gd`>IY#E0MS5W8CZZA zsD^zqY9_By6T7Yk9$_iGR-tIKaQ$_`M-%;yYhJ%qvy$6n$MUk|D}PUv2iN*qD7GhU|ltW$&qyeN5q+tP6!9L!}aU6Q$p!@~P zMjVWBhGu$ZdIk)#lRG(#8Rt#j%ONTK2%@oJ@#z_r^Rq5xGBMU!=$Yyn8Zf+`{EPwPa${0*ZhDEce1PDHGfla6Sg=fn`^qjJcix(0T?~ zPKRY^po}p@hJz1cDvai`DK1LZ%bXm_X)JC5m9u~rLtHkc7Iq4TlM6T`WgRjS3koWA zQ}T25({u8Z5_5EmQqwbwONtUp@{2a_;GDuZnVCz*6`v}7T`XF4At9z$l3oSZJ9#3P zG?IO~`fwQ})#a&4nJGEC$@zI@sYS(^`FX|V8Hpw3>AJa@xv3?U1(PRm$*7|Wr2<`m eCZY=tmdXD)=VP~ z?X%e-90rEMlpMXRVg`ofqGVlTBZw#$14Bb2kjV+8&jD$m&IVyd1_mA=eH0`<@v8M? z14a)<@yV5p_Ke<>cLK?-$v=VQ&B>Nb_KfnAD}iLf1gI5;?feqrDRx_k0Ojwzc< zIMo@sHbDJ;gne=*x4qQA{}2GO4`lZTs5A%1WGNoC$#1wh#8`mBAm@UdYQX^{fFJ}S z?ZP9&)d3Z<n!w4^$ZOdV4*ru zKdM)4tF0wa#>ha=&`8fhc(N_8qzb0I7Q|YH4q&8!KnLgK6TA*wJD^-2?#V)Y>YKy( KCNlzkIs*X70%tn_ diff --git a/bunfig.toml b/bunfig.toml new file mode 100644 index 0000000..5f4659b --- /dev/null +++ b/bunfig.toml @@ -0,0 +1,2 @@ +[test] +preload = "./test/happydom.js" diff --git a/package.json b/package.json index d19880d..be61934 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,16 @@ ".": "./src/qdl.js", "./utils": "./src/utils.js" }, + "//devDependencies": { + "@happy-dom/global-registrator": "happy-dom is used by tests involving browser APIs, like DOMParser" + }, "devDependencies": { + "@happy-dom/global-registrator": "^16.6.0", "@types/bun": "latest" }, + "//dependencies": { + "crc-32": "crc32s are used in the gpt header calculations" + }, "dependencies": { "crc-32": "^1.2.2" } diff --git a/src/xmlParser.spec.js b/src/xmlParser.spec.js new file mode 100644 index 0000000..c5e1aa3 --- /dev/null +++ b/src/xmlParser.spec.js @@ -0,0 +1,209 @@ +import { describe, expect, test } from "bun:test"; + +import { xmlParser } from "./xmlParser"; + + +describe("xmlParser", () => { + const parser = new xmlParser(); + const encoder = new TextEncoder(); + + describe("getResponse", () => { + test("parse a simple response", () => { + const xml = ""; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ value: "ACK" }); + }); + + test("parse configure command response", () => { + const xml = ` + + + `; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ + value: "ACK", + MemoryName: "eMMC", + Verbose: "0", + AlwaysValidate: "0", + MaxDigestTableSizeInBytes: "2048", + MaxPayloadSizeToTargetInBytes: "1048576", + ZLPAwareHost: "1", + SkipStorageInit: "0", + SkipWrite: "0", + }); + }); + + test("parse program command response", () => { + const xml = ` + + + `; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ + value: "ACK", + SECTOR_SIZE_IN_BYTES: "512", + num_partition_sectors: "1000", + physical_partition_number: "0", + start_sector: "0", + }); + }); + + test("parse multiple response documents", () => { + const xml = ` + `; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ value: "DONE" }); // Should contain last response + }); + + test("parse error response", () => { + const xml = ` + + + `; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ + value: "NAK", + error_code: "0x12", + error: "Invalid sector size", + }); + }); + + test("handle malformed XML", () => { + const xml = ""; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({}); + }); + + test("handle special byte sequence", () => { + const xml = Buffer.from("\xf0\xe9\x88\x14", "binary"); + const result = parser.getResponse(xml); + expect(result).toEqual({ value: "ACK" }); + }); + }); + + describe("getLog", () => { + test("parse a simple log", () => { + const xml = ""; + const result = parser.getLog(encoder.encode(xml)); + expect(result).toEqual(["Test message"]); + }); + + test.skip("parse multiple logs", () => { + const xml = ` + + + + + `; + const result = parser.getLog(encoder.encode(xml)); + expect(result).toEqual([ + "Message 1", + "Message 2", + "Message 3", + ]); + }); + + test.skip("parse program operation logs", () => { + const xml = ` + + + + + `; + const result = parser.getLog(encoder.encode(xml)); + expect(result).toEqual([ + "Writing sector 0x1000", + "CRC check passed", + ]); + }); + + test("parse logs with special characters", () => { + const xml = ""; + const result = parser.getLog(encoder.encode(xml)); + expect(result).toEqual(["Test & debug "]); + }); + + test.skip("handle mixed response and log content", () => { + const xml = ` + + + + + `; + const logs = parser.getLog(encoder.encode(xml)); + expect(logs).toEqual([ + "Operation in progress", + "Step 1 complete", + ]); + }); + + test("handle empty value", () => { + const xml = ""; + const result = parser.getLog(encoder.encode(xml)); + expect(result).toEqual([""]); + }); + }); + + describe("Real world protocol examples", () => { + test("parse power command response", () => { + const xml = ""; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ + value: "ACK", + command: "power", + status: "reset", + }); + }); + + test("parse read command response", () => { + const xml = ` + + + `; + const result = parser.getResponse(encoder.encode(xml)); + expect(result).toEqual({ + value: "ACK", + SECTOR_SIZE_IN_BYTES: "512", + num_partition_sectors: "1000", + physical_partition_number: "0", + start_sector: "0", + }); + }); + + test.skip("parse storage info response", () => { + const xml = ` + + + + + + + `; + const logs = parser.getLog(encoder.encode(xml)); + expect(logs).toEqual([ + "UFS Inquiry Command Output: Unipro", + "UFS Total Active LU: 0x3", + "UFS Boot Partition Enabled: 0x1", + "UFS Total Active LU: 0x3", + ]); + }); + }); +}); diff --git a/test/happydom.js b/test/happydom.js new file mode 100644 index 0000000..078d6f2 --- /dev/null +++ b/test/happydom.js @@ -0,0 +1,6 @@ +import { GlobalRegistrator } from "@happy-dom/global-registrator"; + +// FIX for https://github.com/oven-sh/bun/issues/6044 +const oldconsole = console; +GlobalRegistrator.register(); +window.console = oldconsole;