-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1525 Add Vitest and unit tests for roam utils; update package.json #1140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import compileDatalog, { toVar } from "~/utils/compileDatalog"; | ||
| import gatherDatalogVariablesFromClause from "~/utils/gatherDatalogVariablesFromClause"; | ||
| import replaceDatalogVariables from "~/utils/replaceDatalogVariables"; | ||
|
|
||
| describe("compileDatalog", () => { | ||
| it("sanitizes variable names", () => { | ||
| expect(toVar('a b"(c)')).toBe("abc"); | ||
| }); | ||
|
|
||
| it("compiles nested and-clauses", () => { | ||
| const query = compileDatalog({ | ||
| type: "and-clause", | ||
| clauses: [ | ||
| { | ||
| type: "data-pattern", | ||
| arguments: [ | ||
| { type: "variable", value: "node" }, | ||
| { type: "constant", value: ":node/title" }, | ||
| { type: "constant", value: '"Hello"' }, | ||
| ], | ||
| }, | ||
| { | ||
| type: "pred-expr", | ||
| pred: "=", | ||
| arguments: [ | ||
| { type: "variable", value: "node" }, | ||
| { type: "variable", value: "match" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(query).toContain("(and"); | ||
| expect(query).toContain('[?node :node/title "Hello"]'); | ||
| expect(query).toContain("[(= ?node ?match)]"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("gatherDatalogVariablesFromClause", () => { | ||
| it("collects variables from nested clauses", () => { | ||
| const variables = gatherDatalogVariablesFromClause({ | ||
| type: "and-clause", | ||
| clauses: [ | ||
| { | ||
| type: "data-pattern", | ||
| arguments: [ | ||
| { type: "variable", value: "a" }, | ||
| { type: "constant", value: ":rel" }, | ||
| { type: "variable", value: "b" }, | ||
| ], | ||
| }, | ||
| { | ||
| type: "or-join-clause", | ||
| variables: [ | ||
| { type: "variable", value: "c" }, | ||
| { type: "variable", value: "d" }, | ||
| ], | ||
| clauses: [], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(Array.from(variables).sort()).toEqual(["a", "b", "c", "d"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("replaceDatalogVariables", () => { | ||
| it("replaces explicit variable names and function bindings", () => { | ||
| const [clause] = replaceDatalogVariables( | ||
| [{ from: "node", to: "page" }], | ||
| [ | ||
| { | ||
| type: "fn-expr", | ||
| fn: "get", | ||
| arguments: [{ type: "variable", value: "node" }], | ||
| binding: { | ||
| type: "bind-scalar", | ||
| variable: { type: "variable", value: "node" }, | ||
| }, | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| expect(clause.type).toBe("fn-expr"); | ||
| if (clause.type !== "fn-expr") return; | ||
| expect(clause.arguments[0]).toMatchObject({ value: "page" }); | ||
| expect(clause.binding).toMatchObject({ | ||
| variable: { value: "page" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("supports transform replacement for all variables", () => { | ||
| const [clause] = replaceDatalogVariables( | ||
| [{ from: true, to: (v) => `${v}-v2` }], | ||
| [ | ||
| { | ||
| type: "not-join-clause", | ||
| variables: [{ type: "variable", value: "a" }], | ||
| clauses: [ | ||
| { | ||
| type: "data-pattern", | ||
| arguments: [ | ||
| { type: "variable", value: "a" }, | ||
| { type: "constant", value: ":x" }, | ||
| { type: "variable", value: "b" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| expect(clause.type).toBe("not-join-clause"); | ||
| if (clause.type !== "not-join-clause") return; | ||
| expect(clause.variables[0].value).toBe("a-v2"); | ||
| expect(clause.clauses[0]).toMatchObject({ | ||
| arguments: [{ value: "a-v2" }, { value: ":x" }, { value: "b-v2" }], | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type ConditionToDatalogArgs = { | ||
| source: string; | ||
| target: string; | ||
| }; | ||
|
|
||
| vi.mock("~/utils/conditionToDatalog", () => ({ | ||
| default: vi.fn(({ source, target }: ConditionToDatalogArgs) => [ | ||
| { | ||
| type: "data-pattern", | ||
| arguments: [ | ||
| { type: "variable", value: source }, | ||
| { type: "constant", value: ":rel" }, | ||
| /^:in /.test(target) | ||
| ? { type: "variable", value: target.substring(4) } | ||
| : { type: "constant", value: '"value"' }, | ||
| ], | ||
| }, | ||
| ]), | ||
| })); | ||
| vi.mock("~/utils/predefinedSelections", () => ({ | ||
| default: [ | ||
| { | ||
| test: /^created$/, | ||
| pull: () => "(pull ?node [:create/time])", | ||
| mapper: (r: Record<string, string>) => r[":create/time"] || "", | ||
| }, | ||
| ], | ||
| })); | ||
| vi.mock("roamjs-components/util/env", () => ({ getNodeEnv: () => "test" })); | ||
|
|
||
| import fireQuery, { fireQuerySync, getDatalogQuery } from "~/utils/fireQuery"; | ||
|
|
||
| describe("getDatalogQuery", () => { | ||
| it("includes :in variables and de-duplicates expected inputs", async () => { | ||
| const built = getDatalogQuery({ | ||
| conditions: [ | ||
| { | ||
| type: "clause", | ||
| relation: "r", | ||
| source: "node", | ||
| target: ":in title", | ||
| uid: "1", | ||
| not: false, | ||
| }, | ||
| { | ||
| type: "clause", | ||
| relation: "r", | ||
| source: "node", | ||
| target: ":in title", | ||
| uid: "2", | ||
| not: false, | ||
| }, | ||
| ], | ||
| selections: [{ uid: "s1", text: "created", label: "Created" }], | ||
| inputs: { title: "Graph" }, | ||
| }); | ||
|
|
||
| expect(built.query).toContain(":in $ ?title"); | ||
| expect(built.inputs).toEqual(["Graph"]); | ||
| const formatted = await built.formatResult([ | ||
| { ":node/title": "A", ":block/uid": "u1" }, | ||
| { ":block/uid": "u1" }, | ||
| { ":create/time": "123" }, | ||
| ]); | ||
| expect(formatted).toMatchObject({ text: "A", uid: "u1", Created: "123" }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fireQuery", () => { | ||
| beforeEach(() => { | ||
| (globalThis as { window: unknown }).window = { | ||
| roamAlphaAPI: { | ||
| data: { | ||
| async: { | ||
| fast: { | ||
| q: vi | ||
| .fn() | ||
| .mockResolvedValue([ | ||
| [ | ||
| { ":node/title": "Local", ":block/uid": "l1" }, | ||
| { ":block/uid": "l1" }, | ||
| ], | ||
| ]), | ||
| }, | ||
| }, | ||
| backend: { | ||
| q: vi | ||
| .fn() | ||
| .mockResolvedValue([ | ||
| [ | ||
| { ":node/title": "Remote", ":block/uid": "r1" }, | ||
| { ":block/uid": "r1" }, | ||
| ], | ||
| ]), | ||
| }, | ||
|
mdroidian marked this conversation as resolved.
|
||
| fast: { | ||
| q: vi | ||
| .fn() | ||
| .mockReturnValue([ | ||
| [{ ":node/title": "Sync", ":block/uid": "s1" }], | ||
| ]), | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| it("uses backend queries by default and maps output", async () => { | ||
| const results = await fireQuery({ conditions: [], selections: [] }); | ||
| expect(results[0]).toMatchObject({ text: "Remote", uid: "r1" }); | ||
| }); | ||
|
|
||
| it("uses async fast query when local=true", async () => { | ||
| const results = await fireQuery({ | ||
| conditions: [], | ||
| selections: [], | ||
| local: true, | ||
| }); | ||
| expect(results[0]).toMatchObject({ text: "Local", uid: "l1" }); | ||
| }); | ||
|
|
||
| it("returns sync mapped results", () => { | ||
| const results = fireQuerySync({ conditions: [], selections: [] }); | ||
| expect(results).toEqual([{ text: "Sync", uid: "s1" }]); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.