Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
- name: Check TypeScript Types
run: npx turbo check-types

- name: Run Tests
run: npx turbo run test --filter=roam --ui stream

lint-changed-files:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
Expand Down
8 changes: 6 additions & 2 deletions apps/roam/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"publish": "tsx scripts/publish.ts",
"check-types": "tsc --noEmit --skipLibCheck"
"check-types": "tsc --noEmit --skipLibCheck",
"test": "vitest run --config vitest.config.mts",
"test:watch": "vitest --config vitest.config.mts"
},
"license": "Apache-2.0",
"devDependencies": {
Expand All @@ -19,14 +21,16 @@
"@repo/typescript-config": "workspace:*",
"@types/file-saver": "2.0.5",
"@types/nanoid": "2.0.0",
"@types/node": "^20",
"@types/react": "catalog:roam",
"@types/react-dom": "catalog:roam",
"@types/react-vertical-timeline-component": "^3.3.3",
"axios": "^0.27.2",
"dotenv": "^16.0.3",
"esbuild": "0.17.14",
"tailwindcss": "^3.4.17",
"tsx": "^4.19.2"
"tsx": "^4.19.2",
"vitest": "catalog:"
},
"//": "axios dep temporary - need to fix the dep in underlying libraries",
"tags": [
Expand Down
121 changes: 121 additions & 0 deletions apps/roam/src/utils/__tests__/datalogUtils.test.ts
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";
Comment thread
mdroidian marked this conversation as resolved.

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" }],
});
});
});
128 changes: 128 additions & 0 deletions apps/roam/src/utils/__tests__/fireQuery.test.ts
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" },
],
]),
},
Comment thread
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" }]);
});
});
Loading