|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { ResolvedOutput, ResolvedToken } from "../bindings/index.js"; |
| 3 | +import { |
| 4 | + compactTokens, |
| 5 | + outputCardinality, |
| 6 | + outputGuard, |
| 7 | + planOutput, |
| 8 | +} from "./resolve-output-tokens.js"; |
| 9 | + |
| 10 | +function output(overrides: Partial<ResolvedOutput> = {}): ResolvedOutput { |
| 11 | + return { |
| 12 | + name: "out", |
| 13 | + tokens: [], |
| 14 | + branchCondition: [[]], |
| 15 | + listScope: [], |
| 16 | + optional: false, |
| 17 | + ...overrides, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("outputCardinality", () => { |
| 22 | + it("returns 'always' when not optional and no listScope", () => { |
| 23 | + expect(outputCardinality(output())).toBe("always"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("returns 'optional' when optional and no listScope", () => { |
| 27 | + expect(outputCardinality(output({ optional: true }))).toBe("optional"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns 'list' when listScope present and not optional", () => { |
| 31 | + expect(outputCardinality(output({ listScope: ["b1"] }))).toBe("list"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns 'list-optional' when both listScope and optional", () => { |
| 35 | + expect(outputCardinality(output({ listScope: ["b1"], optional: true }))).toBe( |
| 36 | + "list-optional", |
| 37 | + ); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("outputGuard", () => { |
| 42 | + it("returns 'always' for empty branchCondition", () => { |
| 43 | + expect(outputGuard(output({ branchCondition: [] }))).toEqual({ kind: "always" }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns 'always' for single empty path", () => { |
| 47 | + expect(outputGuard(output({ branchCondition: [[]] }))).toEqual({ kind: "always" }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns 'any-of' for non-empty branchCondition", () => { |
| 51 | + const guard = outputGuard( |
| 52 | + output({ |
| 53 | + branchCondition: [ |
| 54 | + [{ kind: "present", binding: "b1" }], |
| 55 | + [ |
| 56 | + { kind: "present", binding: "b2" }, |
| 57 | + { kind: "variant", binding: "u1", variant: "add" }, |
| 58 | + ], |
| 59 | + ], |
| 60 | + }), |
| 61 | + ); |
| 62 | + expect(guard).toEqual({ |
| 63 | + kind: "any-of", |
| 64 | + clauses: [ |
| 65 | + { atoms: [{ kind: "present", binding: "b1" }] }, |
| 66 | + { |
| 67 | + atoms: [ |
| 68 | + { kind: "present", binding: "b2" }, |
| 69 | + { kind: "variant", binding: "u1", variant: "add" }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + ], |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("does NOT collapse a single non-empty path", () => { |
| 77 | + const guard = outputGuard(output({ branchCondition: [[{ kind: "present", binding: "b1" }]] })); |
| 78 | + expect(guard).toEqual({ |
| 79 | + kind: "any-of", |
| 80 | + clauses: [{ atoms: [{ kind: "present", binding: "b1" }] }], |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("compactTokens", () => { |
| 86 | + it("merges consecutive literal tokens", () => { |
| 87 | + const tokens: ResolvedToken[] = [ |
| 88 | + { kind: "literal", value: "a" }, |
| 89 | + { kind: "literal", value: "b" }, |
| 90 | + { kind: "literal", value: "c" }, |
| 91 | + ]; |
| 92 | + expect(compactTokens(tokens)).toEqual([{ kind: "literal", value: "abc" }]); |
| 93 | + }); |
| 94 | + |
| 95 | + it("preserves refs and merges literals around them", () => { |
| 96 | + const tokens: ResolvedToken[] = [ |
| 97 | + { kind: "literal", value: "pre-" }, |
| 98 | + { kind: "literal", value: "fix" }, |
| 99 | + { kind: "ref", binding: "b1" }, |
| 100 | + { kind: "literal", value: ".out" }, |
| 101 | + ]; |
| 102 | + expect(compactTokens(tokens)).toEqual([ |
| 103 | + { kind: "literal", value: "pre-fix" }, |
| 104 | + { kind: "ref", binding: "b1" }, |
| 105 | + { kind: "literal", value: ".out" }, |
| 106 | + ]); |
| 107 | + }); |
| 108 | + |
| 109 | + it("returns empty array for empty input", () => { |
| 110 | + expect(compactTokens([])).toEqual([]); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe("planOutput", () => { |
| 115 | + it("packages cardinality, guard, listScope, and compacted tokens", () => { |
| 116 | + const resolved = output({ |
| 117 | + name: "log", |
| 118 | + tokens: [ |
| 119 | + { kind: "literal", value: "log-" }, |
| 120 | + { kind: "literal", value: "file" }, |
| 121 | + { kind: "ref", binding: "b1" }, |
| 122 | + ], |
| 123 | + branchCondition: [[{ kind: "present", binding: "b2" }]], |
| 124 | + listScope: ["b3"], |
| 125 | + optional: true, |
| 126 | + }); |
| 127 | + const plan = planOutput(resolved); |
| 128 | + expect(plan.name).toBe("log"); |
| 129 | + expect(plan.cardinality).toBe("list-optional"); |
| 130 | + expect(plan.guard).toEqual({ |
| 131 | + kind: "any-of", |
| 132 | + clauses: [{ atoms: [{ kind: "present", binding: "b2" }] }], |
| 133 | + }); |
| 134 | + expect(plan.listScope).toEqual(["b3"]); |
| 135 | + expect(plan.tokens).toEqual([ |
| 136 | + { kind: "literal", value: "log-file" }, |
| 137 | + { kind: "ref", binding: "b1" }, |
| 138 | + ]); |
| 139 | + expect(plan.resolved).toBe(resolved); |
| 140 | + }); |
| 141 | + |
| 142 | + it("preserves ref token's stripExtensions and fallback through compacting", () => { |
| 143 | + const resolved = output({ |
| 144 | + tokens: [ |
| 145 | + { kind: "ref", binding: "b1", stripExtensions: [".nii"], fallback: "x" }, |
| 146 | + ], |
| 147 | + }); |
| 148 | + const plan = planOutput(resolved); |
| 149 | + expect(plan.tokens[0]).toEqual({ |
| 150 | + kind: "ref", |
| 151 | + binding: "b1", |
| 152 | + stripExtensions: [".nii"], |
| 153 | + fallback: "x", |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments