sitegen/framework/incremental.test.ts
chloe caruso 25f01d878c rewrite incremental.ts (#21)
the problems with the original implementation was mostly around error handling. sources had to be tracked manually and provided to each incremental output. the `hasArtifact` check was frequently forgotten. this has been re-abstracted through `incr.work()`, which is given an `io` object. all fs reads and module loads go through this interface, which allows the sources to be properly tracked, even if it throws.

closes #12
2025-08-02 17:31:58 -07:00

56 lines
1.5 KiB
TypeScript

test("trivial case", async () => {
incr.reset();
const file1 = tmpFile("example.txt");
file1.write("one");
async function compilation() {
const first = incr.work({
label: "first compute",
async run (io) {
await setTimeout(1000);
const contents = await io.readFile(file1.path);
return [contents, Math.random()] as const;
}
});
const second = incr.work({
label: "second compute",
wait: first,
async run (io) {
await setTimeout(1000);
return io.readWork(first)[0].toUpperCase();
}
});
const third = incr.work({
label: "third compute",
wait: first,
async run (io) {
await setTimeout(1000);
return io.readWork(first)[1] * 1000;
}
});
return incr.work({
label: "last compute",
wait: [second, third],
async run (io) {
await setTimeout(1000);
return {
second: io.readWork(second),
third: io.readWork(third),
}
}
});
}
const { value: first } = await incr.compile(compilation);
const { value: second } = await incr.compile(compilation);
ASSERT(first === second);
incr.forceInvalidate(file1.path);
const { value: third } = await incr.compile(compilation);
ASSERT(first !== third);
ASSERT(first[0] === third[0]);
});
import * as incr from "./incremental2.ts";
import { beforeEach, test } from "node:test";
import { tmpFile } from "#sitegen/testing";import { setTimeout } from "node:timers/promises";