|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { ParamMap } from "../parser"; |
| 3 | +import { createStreamParser, parse } from "../parser"; |
| 4 | + |
| 5 | +// ── Test schema ────────────────────────────────────────────────────────────── |
| 6 | + |
| 7 | +/** |
| 8 | + * Minimal schema used across tests. |
| 9 | + * |
| 10 | + * Stack takes one param (children), Title takes one (text), |
| 11 | + * Table takes two (columns, rows). These cover the common test cases. |
| 12 | + */ |
| 13 | +const schema: ParamMap = new Map([ |
| 14 | + ["Stack", { params: [{ name: "children", required: true }] }], |
| 15 | + ["Title", { params: [{ name: "text", required: true }] }], |
| 16 | + [ |
| 17 | + "Table", |
| 18 | + { |
| 19 | + params: [ |
| 20 | + { name: "columns", required: true }, |
| 21 | + { name: "rows", required: true }, |
| 22 | + ], |
| 23 | + }, |
| 24 | + ], |
| 25 | +]); |
| 26 | + |
| 27 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 28 | + |
| 29 | +const errors = (input: string) => parse(input, schema).meta.errors; |
| 30 | +const codes = (input: string) => errors(input).map((e) => e.code); |
| 31 | + |
| 32 | +// ── unknown-component ──────────────────────────────────────────────────────── |
| 33 | + |
| 34 | +describe("unknown-component", () => { |
| 35 | + it("reports when component name is not in schema", () => { |
| 36 | + const result = parse('root = DataTable("col")', schema); |
| 37 | + expect(result.meta.errors).toHaveLength(1); |
| 38 | + expect(result.meta.errors[0]).toMatchObject({ |
| 39 | + type: "validation", |
| 40 | + code: "unknown-component", |
| 41 | + component: "DataTable", |
| 42 | + path: "", |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("still renders the element with _args when unknown", () => { |
| 47 | + const result = parse('root = DataTable("col")', schema); |
| 48 | + expect(result.root).not.toBeNull(); |
| 49 | + expect(result.root?.typeName).toBe("DataTable"); |
| 50 | + expect((result.root?.props as any)._args).toEqual(["col"]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("reports all unknown components in a tree", () => { |
| 54 | + const r = codes('root = Stack([Ghost("a")])\n'); |
| 55 | + expect(r).toContain("unknown-component"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("does not report for known component names", () => { |
| 59 | + const result = parse('root = Stack(["hello"])', schema); |
| 60 | + expect(codes('root = Stack(["hello"])')).not.toContain("unknown-component"); |
| 61 | + expect(result.meta.errors).toHaveLength(0); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +// ── excess-args ─────────────────────────────────────────────────────────────── |
| 66 | + |
| 67 | +describe("excess-args", () => { |
| 68 | + it("reports when more args are passed than params", () => { |
| 69 | + const result = parse('root = Title("hello", "extra")', schema); |
| 70 | + expect(result.meta.errors).toHaveLength(1); |
| 71 | + expect(result.meta.errors[0]).toMatchObject({ |
| 72 | + type: "validation", |
| 73 | + code: "excess-args", |
| 74 | + component: "Title", |
| 75 | + path: "", |
| 76 | + }); |
| 77 | + expect(result.meta.errors[0].message).toMatch(/takes 1 arg/); |
| 78 | + }); |
| 79 | + |
| 80 | + it("still renders the component despite excess args", () => { |
| 81 | + const result = parse('root = Title("hello", "extra")', schema); |
| 82 | + expect(result.root).not.toBeNull(); |
| 83 | + expect(result.root?.props.text).toBe("hello"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("does not report when arg count matches param count", () => { |
| 87 | + expect(codes('root = Title("hello")')).not.toContain("excess-args"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("does not report when fewer args than params (handled by missing-required)", () => { |
| 91 | + expect(codes("root = Table([], [])")).not.toContain("excess-args"); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +// ── unresolved references ──────────────────────────────────────────────────── |
| 96 | + |
| 97 | +describe("unresolved references", () => { |
| 98 | + it("tracks unresolved refs in meta.unresolved (one-shot)", () => { |
| 99 | + const result = parse("root = Stack([tbl])", schema); |
| 100 | + expect(result.meta.unresolved).toContain("tbl"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("does not produce errors for unresolved refs", () => { |
| 104 | + const result = parse("root = Stack([tbl])", schema); |
| 105 | + expect(result.meta.errors).toHaveLength(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it("clears unresolved when ref is defined", () => { |
| 109 | + const result = parse('root = Stack([tbl])\ntbl = Title("hello")', schema); |
| 110 | + expect(result.meta.unresolved).toHaveLength(0); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +// ── unresolved references (streaming) ───────────────────────────────────────── |
| 115 | + |
| 116 | +describe("unresolved references (streaming)", () => { |
| 117 | + it("tracks unresolved refs mid-stream without errors", () => { |
| 118 | + const parser = createStreamParser(schema); |
| 119 | + const midResult = parser.push("root = Stack([tbl])\n"); |
| 120 | + expect(midResult.meta.unresolved).toContain("tbl"); |
| 121 | + expect(midResult.meta.errors).toHaveLength(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it("resolves automatically when ref is defined in a later chunk", () => { |
| 125 | + const parser = createStreamParser(schema); |
| 126 | + parser.push("root = Stack([tbl])\n"); |
| 127 | + const result = parser.push('tbl = Title("hello")\n'); |
| 128 | + expect(result.meta.unresolved).toHaveLength(0); |
| 129 | + }); |
| 130 | + |
| 131 | + it("keeps unresolved refs in meta.unresolved at end of stream", () => { |
| 132 | + const parser = createStreamParser(schema); |
| 133 | + parser.push("root = Stack([tbl])\n"); |
| 134 | + const result = parser.getResult(); |
| 135 | + expect(result.meta.unresolved).toContain("tbl"); |
| 136 | + expect(result.meta.errors).toHaveLength(0); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +// ── existing error rules ────────────────────────────────────────────────────── |
| 141 | + |
| 142 | +describe("existing errors carry type and code", () => { |
| 143 | + it("missing-required has correct shape", () => { |
| 144 | + const result = parse("root = Stack()", schema); |
| 145 | + expect(result.meta.errors).toHaveLength(1); |
| 146 | + expect(result.meta.errors[0]).toMatchObject({ |
| 147 | + type: "validation", |
| 148 | + code: "missing-required", |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it("null-required has correct shape", () => { |
| 153 | + const result = parse("root = Stack(null)", schema); |
| 154 | + expect(result.meta.errors).toHaveLength(1); |
| 155 | + expect(result.meta.errors[0]).toMatchObject({ |
| 156 | + type: "validation", |
| 157 | + code: "null-required", |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments