150 lines
5 KiB
TypeScript
150 lines
5 KiB
TypeScript
import { describe, it, expect } from "bun:test";
|
|
import type { AnalyzedCompilationResult } from "@gram-lang/analyzer";
|
|
import { compile } from "@gram-lang/kitchen";
|
|
import {
|
|
composeRecipe,
|
|
loadModuleGraph,
|
|
type ModuleHost,
|
|
type ModuleGraph,
|
|
} from "@gram-lang/modules";
|
|
import {
|
|
affectedOpenImporters,
|
|
moduleInvalidation,
|
|
} from "../src/utils/module-invalidation";
|
|
|
|
/** A minimal in-memory ModuleHost, POSIX-style absolute uris, no real filesystem. */
|
|
function createFakeHost(files: Record<string, string>): ModuleHost {
|
|
return {
|
|
resolve(specifier: string, fromUri: string): string {
|
|
const fromDir = fromUri.slice(0, fromUri.lastIndexOf("/")) || "/";
|
|
const parts = `${fromDir}/${specifier}`.split("/");
|
|
const stack: string[] = [];
|
|
for (const part of parts) {
|
|
if (part === "" || part === ".") continue;
|
|
if (part === "..") stack.pop();
|
|
else stack.push(part);
|
|
}
|
|
return `/${stack.join("/")}`;
|
|
},
|
|
read(uri: string): string {
|
|
if (!(uri in files)) throw new Error(`ENOENT: ${uri}`);
|
|
return files[uri]!;
|
|
},
|
|
};
|
|
}
|
|
|
|
async function chainGraphs(): Promise<Map<string, ModuleGraph>> {
|
|
// A -> B -> C
|
|
const host = createFakeHost({
|
|
"/a.gram": '@use "./b.gram" as &b\n\n## A\n\n[Use] the &b{100g}.\n',
|
|
"/b.gram": '@use "./c.gram" as &c\n\n## B\n\n[Use] the &c{100g}.\n',
|
|
"/c.gram": "## C\n\n[Mix] the @flour{100g}.\n",
|
|
});
|
|
const graphs = new Map<string, ModuleGraph>();
|
|
graphs.set("/a.gram", await loadModuleGraph("/a.gram", host));
|
|
graphs.set("/b.gram", await loadModuleGraph("/b.gram", host));
|
|
return graphs;
|
|
}
|
|
|
|
describe("buildOpenReverseIndex / affectedOpenImporters", () => {
|
|
it("editing the leaf of an open A -> B -> C chain notifies both open importers", async () => {
|
|
const graphs = await chainGraphs();
|
|
const affected = affectedOpenImporters(
|
|
graphs,
|
|
["/a.gram", "/b.gram"],
|
|
"/c.gram",
|
|
);
|
|
expect(new Set(affected)).toEqual(new Set(["/a.gram", "/b.gram"]));
|
|
});
|
|
|
|
it("a dependency that isn't itself an open document is excluded from the result", async () => {
|
|
const graphs = await chainGraphs();
|
|
// Only /a.gram is open; /b.gram's graph still exists in `graphs`
|
|
// (server.ts keeps one per refreshed document), but /b.gram itself
|
|
// isn't part of the *open* set passed in.
|
|
const affected = affectedOpenImporters(graphs, ["/a.gram"], "/c.gram");
|
|
expect(affected).toEqual(["/a.gram"]);
|
|
});
|
|
|
|
it("a document with no importers has no affected dependents", async () => {
|
|
const graphs = await chainGraphs();
|
|
const affected = affectedOpenImporters(
|
|
graphs,
|
|
["/a.gram", "/b.gram"],
|
|
"/a.gram",
|
|
);
|
|
expect(affected).toEqual([]);
|
|
});
|
|
|
|
it("an unrelated open document is unaffected by a change elsewhere", async () => {
|
|
const host = createFakeHost({
|
|
"/solo.gram": "## Solo\n\n[Mix] the @sugar{50g}.\n",
|
|
});
|
|
const graphs = await chainGraphs();
|
|
graphs.set("/solo.gram", await loadModuleGraph("/solo.gram", host));
|
|
const affected = affectedOpenImporters(
|
|
graphs,
|
|
["/a.gram", "/b.gram", "/solo.gram"],
|
|
"/c.gram",
|
|
);
|
|
expect(affected).not.toContain("/solo.gram");
|
|
});
|
|
});
|
|
|
|
describe("moduleInvalidation: the shared yield-measurement cache", () => {
|
|
// B's yield depends on C's: B uses the whole of &c, so a cached measurement
|
|
// of B is only right for the C it was measured with.
|
|
const files = (flour: string): Record<string, string> => ({
|
|
"/a.gram": '@use "./b.gram" as &b\n\n## A\n\n[Use] the &b{100g}.\n',
|
|
"/b.gram":
|
|
'@use "./c.gram" as &c\n\n## B\n\n[Mix] the &c with @sugar{100g}.\n',
|
|
"/c.gram": `## C\n\n[Mix] the @flour{${flour}}.\n`,
|
|
});
|
|
|
|
async function shoppingOfA(
|
|
source: Record<string, string>,
|
|
cache: Map<string, AnalyzedCompilationResult>,
|
|
): Promise<{ graph: ModuleGraph; list: [string, unknown][] }> {
|
|
const graph = await loadModuleGraph("/a.gram", createFakeHost(source));
|
|
const { ast } = composeRecipe(graph, { db: {}, lang: "en", cache });
|
|
const list = compile(ast).shopping_list.map(
|
|
(item) => [item.id, item.qty] as [string, unknown],
|
|
);
|
|
return { graph, list };
|
|
}
|
|
|
|
it("marks the changed module and every module importing it, open or not", async () => {
|
|
const graphs = await chainGraphs();
|
|
const { staleMeasurements, openImporters } = moduleInvalidation(
|
|
graphs,
|
|
["/a.gram"],
|
|
"/c.gram",
|
|
);
|
|
expect(new Set(staleMeasurements)).toEqual(
|
|
new Set(["/c.gram", "/b.gram", "/a.gram"]),
|
|
);
|
|
expect(openImporters).toEqual(["/a.gram"]);
|
|
});
|
|
|
|
it("an edit to C, with B closed, reaches A through B's measurement", async () => {
|
|
const cache = new Map<string, AnalyzedCompilationResult>();
|
|
const { graph } = await shoppingOfA(files("100g"), cache);
|
|
expect(cache.has("/b.gram")).toBe(true);
|
|
|
|
const edited = files("300g");
|
|
const { staleMeasurements } = moduleInvalidation(
|
|
new Map([["/a.gram", graph]]),
|
|
["/a.gram"],
|
|
"/c.gram",
|
|
);
|
|
for (const uri of staleMeasurements) cache.delete(uri);
|
|
|
|
const fresh = await shoppingOfA(edited, new Map());
|
|
const afterEdit = await shoppingOfA(edited, cache);
|
|
expect(afterEdit.list).toEqual(fresh.list);
|
|
expect(afterEdit.list).toEqual([
|
|
["flour", 75],
|
|
["sugar", 25],
|
|
]);
|
|
});
|
|
});
|