|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import compileDatalog, { toVar } from "~/utils/compileDatalog"; |
| 3 | +import gatherDatalogVariablesFromClause from "~/utils/gatherDatalogVariablesFromClause"; |
| 4 | +import replaceDatalogVariables from "~/utils/replaceDatalogVariables"; |
| 5 | + |
| 6 | +describe("compileDatalog", () => { |
| 7 | + it("sanitizes variable names", () => { |
| 8 | + expect(toVar('a b"(c)')).toBe("abc"); |
| 9 | + }); |
| 10 | + |
| 11 | + it("compiles nested and-clauses", () => { |
| 12 | + const query = compileDatalog({ |
| 13 | + type: "and-clause", |
| 14 | + clauses: [ |
| 15 | + { |
| 16 | + type: "data-pattern", |
| 17 | + arguments: [ |
| 18 | + { type: "variable", value: "node" }, |
| 19 | + { type: "constant", value: ":node/title" }, |
| 20 | + { type: "constant", value: '"Hello"' }, |
| 21 | + ], |
| 22 | + }, |
| 23 | + { |
| 24 | + type: "pred-expr", |
| 25 | + pred: "=", |
| 26 | + arguments: [ |
| 27 | + { type: "variable", value: "node" }, |
| 28 | + { type: "variable", value: "match" }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + ], |
| 32 | + }); |
| 33 | + |
| 34 | + expect(query).toContain("(and"); |
| 35 | + expect(query).toContain('[?node :node/title "Hello"]'); |
| 36 | + expect(query).toContain("[(= ?node ?match)]"); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("gatherDatalogVariablesFromClause", () => { |
| 41 | + it("collects variables from nested clauses", () => { |
| 42 | + const variables = gatherDatalogVariablesFromClause({ |
| 43 | + type: "and-clause", |
| 44 | + clauses: [ |
| 45 | + { |
| 46 | + type: "data-pattern", |
| 47 | + arguments: [ |
| 48 | + { type: "variable", value: "a" }, |
| 49 | + { type: "constant", value: ":rel" }, |
| 50 | + { type: "variable", value: "b" }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + type: "or-join-clause", |
| 55 | + variables: [ |
| 56 | + { type: "variable", value: "c" }, |
| 57 | + { type: "variable", value: "d" }, |
| 58 | + ], |
| 59 | + clauses: [], |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + |
| 64 | + expect(Array.from(variables).sort()).toEqual(["a", "b", "c", "d"]); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("replaceDatalogVariables", () => { |
| 69 | + it("replaces explicit variable names and function bindings", () => { |
| 70 | + const [clause] = replaceDatalogVariables( |
| 71 | + [{ from: "node", to: "page" }], |
| 72 | + [ |
| 73 | + { |
| 74 | + type: "fn-expr", |
| 75 | + fn: "identity", |
| 76 | + arguments: [{ type: "variable", value: "node" }], |
| 77 | + binding: { |
| 78 | + type: "bind-scalar", |
| 79 | + variable: { type: "variable", value: "node" }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + ); |
| 84 | + |
| 85 | + expect(clause.type).toBe("fn-expr"); |
| 86 | + if (clause.type !== "fn-expr") return; |
| 87 | + expect(clause.arguments[0]).toMatchObject({ value: "page" }); |
| 88 | + expect(clause.binding).toMatchObject({ |
| 89 | + variable: { value: "page" }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("supports transform replacement for all variables", () => { |
| 94 | + const [clause] = replaceDatalogVariables( |
| 95 | + [{ from: true, to: (v) => `${v}-v2` }], |
| 96 | + [ |
| 97 | + { |
| 98 | + type: "not-join-clause", |
| 99 | + variables: [{ type: "variable", value: "a" }], |
| 100 | + clauses: [ |
| 101 | + { |
| 102 | + type: "data-pattern", |
| 103 | + arguments: [ |
| 104 | + { type: "variable", value: "a" }, |
| 105 | + { type: "constant", value: ":x" }, |
| 106 | + { type: "variable", value: "b" }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + ], |
| 112 | + ); |
| 113 | + |
| 114 | + expect(clause.type).toBe("not-join-clause"); |
| 115 | + if (clause.type !== "not-join-clause") return; |
| 116 | + expect(clause.variables[0].value).toBe("a-v2"); |
| 117 | + expect(clause.clauses[0]).toMatchObject({ |
| 118 | + arguments: [{ value: "a-v2" }, { value: ":x" }, { value: "b-v2" }], |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments