repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/ultra/__tests__/interp.test.ts
RawFile outline
- fixture line 15
- resolveTemplate line 37
- substitutes {args.NAME} line 38
- renders {item} string verbatim line 44
- renders {item} object as compact JSON line 48
- renders a non-string arg (number) as JSON line 55
- substitutes multiple tokens in one template line 59
- renders prior phase failures line 65
- throws SpecValidationError on an unknown token line 71
- resolveSelector line 78
- retains nulls positionally for plain {PHASE.results} line 79
- flatten {PHASE.results[].FIELD[]} collects arrays and skips null elements + missing FIELDs line 90
- {EXPR | where FIELD} keeps truthy FIELD, skips null elements + falsy + missing FIELD line 96
- resolves and filters {PHASE.failures} line 106
- rejects an unknown phase failure reference line 115
- resolves {args.NAME} line 121
- resolves {item} line 125
- throws SpecValidationError on an unknown token {foo.bar | map x} line 129
- throws SpecValidationError on an unknown filter line 135
- throws SpecValidationError on a non-brace-wrapped selector line 141
- throws SpecValidationError on a phase reference that is not present line 147
- skips a non-array FIELD under flatten line 153
- line-197 shared-fixture property line 161
- the same null is retained for plain results but dropped under flatten and where line 162
- resolveOver line 186
- returns an array literal unchanged line 187
- resolves failures as a recovery fanout line 192
- resolves an interpolation expression to its array line 198
- throws SpecValidationError when an interpolation does not resolve to an array line 207
- throws SpecValidationError when over is neither an array nor a string line 212
import { describe, expect, it } from "vitest";
import {
type InterpContext,
resolveOver,
resolveSelector,
resolveTemplate,
SpecValidationError,
} from "../interp.ts";
// One shared null-containing fixture drives the "line-197" property:
// the same `null` is RETAINED positionally for plain `{r.results}` but
// DROPPED under flatten `{r.results[].findings[]}` and `{... | where real}`.
// It also carries a missing-`findings` row and a missing-`real` row so the
// "missing FIELD" skip is exercised for both flatten and where.
function fixture(): InterpContext {
return {
args: { topic: "security", count: 3, tags: ["a", "b"] },
item: "performance",
results: {
r: [
{ real: true, findings: ["a1", "a2"] }, // index 0: kept, 2 findings
null, // index 1: the retained/dropped null
{ real: false, findings: ["b1"] }, // index 2: real falsy
{ real: true }, // index 3: truthy but MISSING findings
{ findings: ["c1"] }, // index 4: has findings but MISSING real
],
},
failures: {
r: [
{ code: "transport", retryable: true, message: "rate limited" },
{ code: "auth", retryable: false, message: "missing key" },
],
},
};
}
describe("resolveTemplate", () => {
it("substitutes {args.NAME}", () => {
expect(resolveTemplate("topic is {args.topic}", fixture())).toBe(
"topic is security",
);
});
it("renders {item} string verbatim", () => {
expect(resolveTemplate("do {item}", fixture())).toBe("do performance");
});
it("renders {item} object as compact JSON", () => {
const ctx: InterpContext = { item: { file: "a.ts", line: 3 } };
expect(resolveTemplate("Finding: {item}", ctx)).toBe(
'Finding: {"file":"a.ts","line":3}',
);
});
it("renders a non-string arg (number) as JSON", () => {
expect(resolveTemplate("n={args.count}", fixture())).toBe("n=3");
});
it("substitutes multiple tokens in one template", () => {
expect(resolveTemplate("{args.topic}/{item}", fixture())).toBe(
"security/performance",
);
});
it("renders prior phase failures", () => {
expect(resolveTemplate("repair {r.failures}", fixture())).toContain(
'"code":"transport"',
);
});
it("throws SpecValidationError on an unknown token", () => {
expect(() => resolveTemplate("x {foo.bar | map x} y", fixture())).toThrow(
SpecValidationError,
);
});
});
describe("resolveSelector", () => {
it("retains nulls positionally for plain {PHASE.results}", () => {
const out = resolveSelector("{r.results}", fixture());
expect(Array.isArray(out)).toBe(true);
expect(out).toHaveLength(5);
expect((out as unknown[])[1]).toBeNull();
expect((out as unknown[])[0]).toEqual({
real: true,
findings: ["a1", "a2"],
});
});
it("flatten {PHASE.results[].FIELD[]} collects arrays and skips null elements + missing FIELDs", () => {
// null (idx1) skipped; idx3 missing findings skipped; rest flattened.
const out = resolveSelector("{r.results[].findings[]}", fixture());
expect(out).toEqual(["a1", "a2", "b1", "c1"]);
});
it("{EXPR | where FIELD} keeps truthy FIELD, skips null elements + falsy + missing FIELD", () => {
// idx0 real:true kept; idx1 null skipped; idx2 real:false skipped;
// idx3 real:true kept; idx4 missing real skipped.
const out = resolveSelector("{r.results | where real}", fixture());
expect(out).toEqual([
{ real: true, findings: ["a1", "a2"] },
{ real: true },
]);
});
it("resolves and filters {PHASE.failures}", () => {
expect(resolveSelector("{r.failures}", fixture())).toHaveLength(2);
expect(
resolveSelector("{r.failures | where retryable}", fixture()),
).toEqual([
{ code: "transport", retryable: true, message: "rate limited" },
]);
});
it("rejects an unknown phase failure reference", () => {
expect(() => resolveSelector("{missing.failures}", fixture())).toThrow(
SpecValidationError,
);
});
it("resolves {args.NAME}", () => {
expect(resolveSelector("{args.tags}", fixture())).toEqual(["a", "b"]);
});
it("resolves {item}", () => {
expect(resolveSelector("{item}", fixture())).toBe("performance");
});
it("throws SpecValidationError on an unknown token {foo.bar | map x}", () => {
expect(() => resolveSelector("{foo.bar | map x}", fixture())).toThrow(
SpecValidationError,
);
});
it("throws SpecValidationError on an unknown filter", () => {
expect(() =>
resolveSelector("{r.results | filter real}", fixture()),
).toThrow(SpecValidationError);
});
it("throws SpecValidationError on a non-brace-wrapped selector", () => {
expect(() => resolveSelector("r.results", fixture())).toThrow(
SpecValidationError,
);
});
it("throws SpecValidationError on a phase reference that is not present", () => {
expect(() => resolveSelector("{missing.results}", fixture())).toThrow(
SpecValidationError,
);
});
it("skips a non-array FIELD under flatten", () => {
const ctx: InterpContext = {
results: { r: [{ findings: "not-an-array" }, { findings: ["ok"] }] },
};
expect(resolveSelector("{r.results[].findings[]}", ctx)).toEqual(["ok"]);
});
});
describe("line-197 shared-fixture property", () => {
it("the same null is retained for plain results but dropped under flatten and where", () => {
const ctx = fixture();
const plain = resolveSelector("{r.results}", ctx) as unknown[];
const flat = resolveSelector("{r.results[].findings[]}", ctx) as unknown[];
const filtered = resolveSelector(
"{r.results | where real}",
ctx,
) as unknown[];
// plain: null present positionally
expect(plain).toHaveLength(5);
expect(plain[1]).toBeNull();
// flatten + where: null never appears
expect(flat.some((x) => x === null)).toBe(false);
expect(filtered.some((x) => x === null)).toBe(false);
expect(flat).toEqual(["a1", "a2", "b1", "c1"]);
expect(filtered).toEqual([
{ real: true, findings: ["a1", "a2"] },
{ real: true },
]);
});
});
describe("resolveOver", () => {
it("returns an array literal unchanged", () => {
const lit = ["correctness", "security", "performance"];
expect(resolveOver(lit, {})).toEqual(lit);
});
it("resolves failures as a recovery fanout", () => {
expect(resolveOver("{r.failures}", fixture())).toEqual(
fixture().failures?.r,
);
});
it("resolves an interpolation expression to its array", () => {
expect(resolveOver("{r.results[].findings[]}", fixture())).toEqual([
"a1",
"a2",
"b1",
"c1",
]);
});
it("throws SpecValidationError when an interpolation does not resolve to an array", () => {
// {item} resolves to a string, not an array
expect(() => resolveOver("{item}", fixture())).toThrow(SpecValidationError);
});
it("throws SpecValidationError when over is neither an array nor a string", () => {
expect(() => resolveOver(42, fixture())).toThrow(SpecValidationError);
});
});