test: missing error tests for evaluation errors

Add explicit unit tests that construct invalid AST nodes to verify that the core `evaluate()` function throws `EvalError` and `XRefError` exceptions correctly when hitting evaluation issues like unresolved variables or broken cross-references.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-08-09 10:26:36 +00:00
parent c958f30662
commit fe7001d715

View file

@ -1,8 +1,9 @@
import { describe, it, expect, beforeAll } from "vitest";
import { SumEngine } from "./index";
import { qty } from "./types";
import type { XRefResolution } from "./evaluator";
import { formatValue } from "./index";
import { qty, EvalError, XRefError, Decimal } from "./types";
import type { XRefResolution, EvalCtx } from "./evaluator";
import { evaluate } from "./evaluator";
import { formatValue } from "./formatter";
let eng: SumEngine;
@ -423,3 +424,24 @@ describe("cross-sheet references", () => {
expect(r[0].assign).toBe("rent");
});
});
describe("evaluation errors", () => {
it("throws EvalError for unknown variable", () => {
const ctx = {
vars: new Map(),
reg: eng.reg,
historicalRates: {},
} as unknown as EvalCtx;
expect(() => evaluate({ k: "var", name: "unknown" }, ctx)).toThrowError(EvalError);
});
it("throws XRefError for unresolved xref", () => {
const ctx = {
vars: new Map(),
reg: eng.reg,
historicalRates: {},
resolveXRef: (sheet: string, key: string) => ({ ok: false, reason: "xref error" }),
} as unknown as EvalCtx;
expect(() => evaluate({ k: "xref", sheet: "Sheet", key: "key" }, ctx)).toThrowError(XRefError);
});
});