diff --git a/.changeset/wrapped-parsers-0-2.md b/.changeset/wrapped-parsers-0-2.md new file mode 100644 index 0000000..7e5f374 --- /dev/null +++ b/.changeset/wrapped-parsers-0-2.md @@ -0,0 +1,18 @@ +--- +"@cosyte/cli": patch +--- + +The command now installs `@cosyte/astm`, `@cosyte/dicom`, `@cosyte/ncpdp`, `@cosyte/terminology` and `@cosyte/x12` at `^0.2.0`. No command, option, exit code or `CLI_*` code changes. + +`cosyte fmt` still writes the canonical form for ASTM and NCPDP (Telecom and SCRIPT). Those parsers now write a message back as it was read by default, so `fmt` asks them for the canonical form explicitly. + +A code the CLI passes through from a wrapped parser, in parentheses after `CLI_PARSE_FAILED` on stderr when an input cannot be parsed, is now that parser's 0.2 code. The exit code (`65`) and the `CLI_*` code do not change. The renamed codes are: + +- `EMPTY_INPUT` from ASTM is now `ASTM_EMPTY_INPUT`. +- `EMPTY_INPUT` from NCPDP is now `NCPDP_SCRIPT_EMPTY_INPUT` (SCRIPT) or `NCPDP_TELECOM_EMPTY_INPUT` (Telecom). +- `EMPTY_INPUT` from DICOM is now `DICOM_EMPTY_INPUT`. +- `NOT_DICOM_PART_10` is now `DICOM_NOT_PART_10`. +- `INVALID_FILE_META` is now `DICOM_INVALID_FILE_META`. +- `UNSUPPORTED_TRANSFER_SYNTAX` from DICOM is now `DICOM_UNSUPPORTED_TRANSFER_SYNTAX`. + +A script that matched one of the earlier strings should match the new one. diff --git a/package.json b/package.json index b8fe626..2b33397 100644 --- a/package.json +++ b/package.json @@ -166,18 +166,18 @@ }, "dependencies": { "@cosyte/hl7": "^0.1.10", - "@cosyte/terminology": "^0.1.6" + "@cosyte/terminology": "^0.2.0" }, "optionalDependencies": { - "@cosyte/astm": "^0.1.7", + "@cosyte/astm": "^0.2.0", "@cosyte/ccda": "^0.1.6", "@cosyte/deid": "^0.2.4", - "@cosyte/dicom": "^0.1.13", + "@cosyte/dicom": "^0.2.0", "@cosyte/fhir": "^0.1.11", "@cosyte/mllp": "^0.1.9", - "@cosyte/ncpdp": "^0.1.7", + "@cosyte/ncpdp": "^0.2.0", "@cosyte/transform": "^0.2.2", - "@cosyte/x12": "^0.1.12", + "@cosyte/x12": "^0.2.0", "@modelcontextprotocol/sdk": "^1.32.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 30c3597..38e1641 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,8 +19,8 @@ importers: specifier: ^0.1.10 version: 0.1.10(fast-check@4.10.2) '@cosyte/terminology': - specifier: ^0.1.6 - version: 0.1.6 + specifier: ^0.2.0 + version: 0.2.0 devDependencies: '@arethetypeswrong/cli': specifier: 0.18.5 @@ -45,7 +45,7 @@ importers: version: 0.1.17 '@cosyte/synth': specifier: ^0.1.5 - version: 0.1.5(@cosyte/astm@0.1.7)(@cosyte/ccda@0.1.6)(@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.1.13)(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2)))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2)) + version: 0.1.5(@cosyte/astm@0.2.0)(@cosyte/ccda@0.1.6)(@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.2.0(fast-check@4.10.2))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2)))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2)) '@cosyte/test-utils': specifier: ^0.1.4 version: 0.1.4(fast-check@4.10.2) @@ -87,17 +87,17 @@ importers: version: 5.1.0(@types/node@22.20.1)(@vitest/coverage-v8@5.1.0)(vite@8.3.6(@types/node@22.20.1)(esbuild@0.28.2)(tsx@4.23.15)(yaml@2.9.1)) optionalDependencies: '@cosyte/astm': - specifier: ^0.1.7 - version: 0.1.7 + specifier: ^0.2.0 + version: 0.2.0 '@cosyte/ccda': specifier: ^0.1.6 version: 0.1.6 '@cosyte/deid': specifier: ^0.2.4 - version: 0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.1.13)(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2)) + version: 0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.2.0(fast-check@4.10.2))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2)) '@cosyte/dicom': - specifier: ^0.1.13 - version: 0.1.13 + specifier: ^0.2.0 + version: 0.2.0(fast-check@4.10.2) '@cosyte/fhir': specifier: ^0.1.11 version: 0.1.11(fast-check@4.10.2) @@ -105,14 +105,14 @@ importers: specifier: ^0.1.9 version: 0.1.9(@cosyte/hl7@0.1.10(fast-check@4.10.2))(fast-check@4.10.2) '@cosyte/ncpdp': - specifier: ^0.1.7 - version: 0.1.7 + specifier: ^0.2.0 + version: 0.2.0(fast-check@4.10.2) '@cosyte/transform': specifier: ^0.2.2 version: 0.2.2(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2)) '@cosyte/x12': - specifier: ^0.1.12 - version: 0.1.12(fast-check@4.10.2) + specifier: ^0.2.0 + version: 0.2.0(fast-check@4.10.2) '@modelcontextprotocol/sdk': specifier: ^1.32.2 version: 1.32.2(zod@4.6.7) @@ -422,8 +422,8 @@ packages: resolution: {integrity: sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==} engines: {node: '>=0.1.90'} - '@cosyte/astm@0.1.7': - resolution: {integrity: sha512-yl6nX9TM6RvxKzmU3k1Ers3drwr+sR8B3t0q1omoTQClfKktNHgIGkWjEzpNHumwKEdZm9mhoOCRQdqSGLQTfQ==} + '@cosyte/astm@0.2.0': + resolution: {integrity: sha512-8XjYK0KVOL86bAe0kg3BczV41t5C1fOAFB4bZUouREOhDFGh410nmir2i1xEZ+EVtJ/UQTnZRtA4QZ3gNws8cA==} engines: {node: '>=22.0.0'} '@cosyte/bench@0.1.1': @@ -459,9 +459,14 @@ packages: '@cosyte/x12': optional: true - '@cosyte/dicom@0.1.13': - resolution: {integrity: sha512-5AwnJc6Ps0r2/qz7ohrfj3i+xQ/ACuo3yWWvdksQMowMqWvOHezG3tJCMjUw5Ax6OfSrq4odfd7wfxDbo1x2fA==} + '@cosyte/dicom@0.2.0': + resolution: {integrity: sha512-tRH/RULYTBQ8dW2LuXlN1bRsjw9KR4rjC/lKMRQ8so80a4OdGL16JOLE8fC4iS2uoDgwILoQg7synwqsu5plmA==} engines: {node: '>=22.0.0'} + peerDependencies: + fast-check: ^4.0.0 + peerDependenciesMeta: + fast-check: + optional: true '@cosyte/eslint-config@0.1.4': resolution: {integrity: sha512-o3qNe5RY+iRtA//OD3pokJmZ6lyQnr2FBZeWy6hhc9z39IpW3wINE5WHyqUuWfpzZOOy61s2zR5nIh4+ld061w==} @@ -500,9 +505,14 @@ packages: fast-check: optional: true - '@cosyte/ncpdp@0.1.7': - resolution: {integrity: sha512-jcbPL1xxfSJ4F875Ax7Klmc8UyHidZepToJYBWz0Noi6fLiHaoXVfulOWCNEv+uCX6Dp/Tp5NIW0uguNDZ3SKA==} + '@cosyte/ncpdp@0.2.0': + resolution: {integrity: sha512-fyOJjQzH4BPqVMHyAms9QC3UzRWoLeR+/io0bzh3bM+loFA57tuBy3JZMac/W/Av6y25elmO6yqf+Biaz9meew==} engines: {node: '>=22.0.0'} + peerDependencies: + fast-check: ^4.0.0 + peerDependenciesMeta: + fast-check: + optional: true '@cosyte/prettier-config@0.1.2': resolution: {integrity: sha512-bpcOjNAU+IBscFxZk7CBODgd7VRwOirg8LlffJLAQUTobI1JunbvAZa98u1BNv+/m5ILMGVNFopGW9HPNA2H5Q==} @@ -544,8 +554,8 @@ packages: '@cosyte/x12': optional: true - '@cosyte/terminology@0.1.6': - resolution: {integrity: sha512-ZiIU4/9YWuKpMt9w/JeDvrSN7CVdgIxLyMfKszoOPE4cXHR33iUszKul1gIu8PT3fjAIeAusq9kM74ZrQ79ScA==} + '@cosyte/terminology@0.2.0': + resolution: {integrity: sha512-wD+/SbrZ3KUz9698XRXncCzHOorxXJqcsFQrBVrKJCFzOBZRmaPu+33h4rUqKH11P5WL9rRalK/RxKsxyJfGUA==} engines: {node: '>=22.0.0'} '@cosyte/test-utils@0.1.4': @@ -590,8 +600,8 @@ packages: typescript: optional: true - '@cosyte/x12@0.1.12': - resolution: {integrity: sha512-gcGkYgpKpYFbX0Z+HJHtKpazJXfO4PdyHrOSj+s7AqjafRUAhySQP3wXCRgnXYLj1DjJHWY67cARfOZ94dc31Q==} + '@cosyte/x12@0.2.0': + resolution: {integrity: sha512-umpeM3W1W0TyNHih3bFGxYIYpEnGavmD/Zuijt9p4rWf7CiCZrxouRitl9ju3sGVNkimzUDqkE7bR6gMdbRAoQ==} engines: {node: '>=22.0.0'} peerDependencies: fast-check: ^4.0.0 @@ -3630,7 +3640,7 @@ snapshots: '@colors/colors@1.5.0': optional: true - '@cosyte/astm@0.1.7': + '@cosyte/astm@0.2.0': optional: true '@cosyte/bench@0.1.1': {} @@ -3640,17 +3650,19 @@ snapshots: '@xmldom/xmldom': 0.9.12 optional: true - '@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.1.13)(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2))': + '@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.2.0(fast-check@4.10.2))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2))': optionalDependencies: '@cosyte/ccda': 0.1.6 - '@cosyte/dicom': 0.1.13 + '@cosyte/dicom': 0.2.0(fast-check@4.10.2) '@cosyte/fhir': 0.1.11(fast-check@4.10.2) '@cosyte/hl7': 0.1.10(fast-check@4.10.2) - '@cosyte/ncpdp': 0.1.7 - '@cosyte/x12': 0.1.12(fast-check@4.10.2) + '@cosyte/ncpdp': 0.2.0(fast-check@4.10.2) + '@cosyte/x12': 0.2.0(fast-check@4.10.2) optional: true - '@cosyte/dicom@0.1.13': + '@cosyte/dicom@0.2.0(fast-check@4.10.2)': + optionalDependencies: + fast-check: 4.10.2 optional: true '@cosyte/eslint-config@0.1.4(eslint@10.14.0)(typescript@5.9.3)': @@ -3679,7 +3691,9 @@ snapshots: fast-check: 4.10.2 optional: true - '@cosyte/ncpdp@0.1.7': + '@cosyte/ncpdp@0.2.0(fast-check@4.10.2)': + optionalDependencies: + fast-check: 4.10.2 optional: true '@cosyte/prettier-config@0.1.2': {} @@ -3718,17 +3732,17 @@ snapshots: '@cosyte/script-utils@0.1.17': {} - '@cosyte/synth@0.1.5(@cosyte/astm@0.1.7)(@cosyte/ccda@0.1.6)(@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.1.13)(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2)))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2))': + '@cosyte/synth@0.1.5(@cosyte/astm@0.2.0)(@cosyte/ccda@0.1.6)(@cosyte/deid@0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.2.0(fast-check@4.10.2))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2)))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2))': optionalDependencies: - '@cosyte/astm': 0.1.7 + '@cosyte/astm': 0.2.0 '@cosyte/ccda': 0.1.6 - '@cosyte/deid': 0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.1.13)(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.1.7)(@cosyte/x12@0.1.12(fast-check@4.10.2)) + '@cosyte/deid': 0.2.4(@cosyte/ccda@0.1.6)(@cosyte/dicom@0.2.0(fast-check@4.10.2))(@cosyte/fhir@0.1.11(fast-check@4.10.2))(@cosyte/hl7@0.1.10(fast-check@4.10.2))(@cosyte/ncpdp@0.2.0(fast-check@4.10.2))(@cosyte/x12@0.2.0(fast-check@4.10.2)) '@cosyte/fhir': 0.1.11(fast-check@4.10.2) '@cosyte/hl7': 0.1.10(fast-check@4.10.2) - '@cosyte/ncpdp': 0.1.7 - '@cosyte/x12': 0.1.12(fast-check@4.10.2) + '@cosyte/ncpdp': 0.2.0(fast-check@4.10.2) + '@cosyte/x12': 0.2.0(fast-check@4.10.2) - '@cosyte/terminology@0.1.6': {} + '@cosyte/terminology@0.2.0': {} '@cosyte/test-utils@0.1.4(fast-check@4.10.2)': dependencies: @@ -3757,7 +3771,7 @@ snapshots: '@stryker-mutator/vitest-runner': 10.1.1(@stryker-mutator/core@10.1.1(@types/node@22.20.1))(vitest@5.1.0) typescript: 5.9.3 - '@cosyte/x12@0.1.12(fast-check@4.10.2)': + '@cosyte/x12@0.2.0(fast-check@4.10.2)': optionalDependencies: fast-check: 4.10.2 optional: true diff --git a/src/core/parsers.ts b/src/core/parsers.ts index ae371db..31a8bbe 100644 --- a/src/core/parsers.ts +++ b/src/core/parsers.ts @@ -581,7 +581,7 @@ export async function inspectFormat( return { format: "dicom", sopClassUid: ds.fileMeta?.mediaStorageSOPClassUID ?? null, - /* v8 ignore next -- parseDicom refuses an input with no File Meta Transfer Syntax UID (INVALID_FILE_META or NOT_DICOM_PART_10), so a parsed file always carries one */ + /* v8 ignore next -- parseDicom refuses an input with no File Meta Transfer Syntax UID (DICOM_INVALID_FILE_META or DICOM_NOT_PART_10), so a parsed file always carries one */ transferSyntaxUid: ds.fileMeta?.transferSyntaxUID ?? null, elementCount: ds.elements().length, warningCount, @@ -674,7 +674,10 @@ export async function fmtFormat(format: CosyteFormat, bytes: Uint8Array): Promis () => import("@cosyte/astm"), ); const msg = parseAstmRecords(decode(bytes)); - return { output: serializeAstmRecords(msg), warningCount: msg.warnings.length }; + return { + output: serializeAstmRecords(msg, { normalize: true }), + warningCount: msg.warnings.length, + }; } case "ccda": { const { parseCcda, serializeCcda } = await loadOptional("ccda", () => import("@cosyte/ccda")); @@ -688,10 +691,16 @@ export async function fmtFormat(format: CosyteFormat, bytes: Uint8Array): Promis ); if (!isNcpdpScript(bytes)) { const tx = parseTelecom(Buffer.from(bytes)); - return { output: serializeTelecom(tx), warningCount: tx.warnings.length }; + return { + output: serializeTelecom(tx, { normalize: true }), + warningCount: tx.warnings.length, + }; } const msg = parseScript(decode(bytes)); - return { output: serializeScript(msg), warningCount: msg.warnings.length }; + return { + output: serializeScript(msg, { normalize: true }), + warningCount: msg.warnings.length, + }; } case "dicom": case "mllp": diff --git a/test/formats.test.ts b/test/formats.test.ts index fd4a6dd..2edf670 100644 --- a/test/formats.test.ts +++ b/test/formats.test.ts @@ -95,6 +95,15 @@ describe("astm: parse / inspect / fmt / validate", () => { expect(r.stdout.startsWith("H")).toBe(true); }); + it("fmt writes the canonical delimiter set, not the one the input was read with", async () => { + // The same two records with `!` as the field delimiter: fmt is the normalizer, so it must write + // them back with `|`, exactly as the library's canonical form. + const input = enc.encode("H!\\^&\rL!1!N\r"); + const r = await run(["fmt", "f.astm"], bytesDeps(input)); + expect(r.exit).toBe(EXIT.OK); + expect(r.stdout).toBe("H|\\^&\rL|1|N\r\n"); + }); + it("validate returns a verdict", async () => { const r = await run(["validate", "f.astm"], bytesDeps(ASTM)); expect([EXIT.OK, EXIT.INVALID]).toContain(r.exit); @@ -172,6 +181,18 @@ describe("ncpdp: parse / inspect / fmt / validate (SCRIPT)", () => { expect(r.stdout).toContain("Message"); }); + it("fmt writes one canonical form for inputs that differ only in markup", async () => { + // Whitespace inside a tag changes the text but not the message, so both format alike. + const spaced = enc.encode( + Buffer.from(NCPDP).toString("utf8").replace("F", "F"), + ); + const a = await run(["fmt", "rx.xml"], bytesDeps(NCPDP)); + const b = await run(["fmt", "rx.xml"], bytesDeps(spaced)); + expect(a.exit).toBe(EXIT.OK); + expect(b.exit).toBe(EXIT.OK); + expect(b.stdout).toBe(a.stdout); + }); + it("validate returns a verdict", async () => { const r = await run(["validate", "rx.xml"], bytesDeps(NCPDP)); expect([EXIT.OK, EXIT.INVALID]).toContain(r.exit); @@ -233,6 +254,19 @@ describe("ncpdp: parse / inspect / fmt / validate (Telecom vD.0, a B1 billing cl expect(r.stdout).toBe(`${Buffer.from(TELECOM).toString("latin1")}\n`); }); + it("fmt normalizes a Telecom input that is not in canonical form", async () => { + // An empty field separator before the AM04 segment separator: the canonical emit drops it, so fmt + // must give back the fixture, not the input it read. + const loose = Buffer.from( + Buffer.from(TELECOM).toString("latin1").replace("\x1eAM04", "\x1c\x1eAM04"), + "latin1", + ); + expect(loose.equals(Buffer.from(TELECOM))).toBe(false); + const r = await run(["fmt", "b1.ncpdp"], bytesDeps(loose)); + expect(r.exit).toBe(EXIT.OK); + expect(r.stdout).toBe(`${Buffer.from(TELECOM).toString("latin1")}\n`); + }); + it("validate returns a clean verdict", async () => { const r = await run(["validate", "b1.ncpdp", "--json"], bytesDeps(TELECOM)); expect(r.exit).toBe(EXIT.OK); diff --git a/test/sibling-releases.test.ts b/test/sibling-releases.test.ts index fa55a00..81da43f 100644 --- a/test/sibling-releases.test.ts +++ b/test/sibling-releases.test.ts @@ -13,14 +13,14 @@ import { describe, expect, it } from "vitest"; */ const FLOORS: Readonly> = { "@cosyte/hl7": "0.1.10", - "@cosyte/terminology": "0.1.6", - "@cosyte/astm": "0.1.7", + "@cosyte/terminology": "0.2.0", + "@cosyte/astm": "0.2.0", "@cosyte/ccda": "0.1.6", - "@cosyte/dicom": "0.1.13", + "@cosyte/dicom": "0.2.0", "@cosyte/fhir": "0.1.11", "@cosyte/mllp": "0.1.9", - "@cosyte/ncpdp": "0.1.7", - "@cosyte/x12": "0.1.12", + "@cosyte/ncpdp": "0.2.0", + "@cosyte/x12": "0.2.0", "@cosyte/transform": "0.2.2", "@cosyte/deid": "0.2.4", };