Skip to content
Merged
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
37 changes: 23 additions & 14 deletions src/platform/adapters/runtime/bun/filesystem-adapter.bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import { join } from "node:path";
import { BunFileSystemAdapter } from "./filesystem-adapter.ts";
import { getBunRuntime } from "./types.ts";

function delay(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

describe("BunFileSystemAdapter native integration", () => {
it("reads, writes, and watches through the real Bun runtime", async () => {
if (!getBunRuntime()) return;
const root = await mkdtemp(join(tmpdir(), "veryfront-bun-fs-"));
const adapter = new BunFileSystemAdapter();
let watcher: ReturnType<BunFileSystemAdapter["watch"]> | undefined;
let timeoutId: ReturnType<typeof setTimeout> | undefined;

try {
const file = join(root, "file.txt");
const watchedFile = join(root, "watched.txt");
await adapter.writeFile(file, "hello");
assertEquals(await adapter.readFile(file), "hello");
assertEquals((await adapter.readFileBytes(file)).length, 5);
Expand Down Expand Up @@ -96,26 +94,37 @@ describe("BunFileSystemAdapter native integration", () => {
}

watcher = adapter.watch(root, { recursive: false });
const eventPromise = watcher[Symbol.asyncIterator]().next();
const iterator = watcher[Symbol.asyncIterator]();
assertExists(watcher.ready);
await watcher.ready;
await adapter.writeFile(file, "updated");
const event = await Promise.race([
eventPromise,
delay(3_000).then(() => {
throw new Error("Bun filesystem watcher integration timed out");
}),
]);
assertEquals(event.done, false);
const observed = (async () => {
while (true) {
const result = await iterator.next();
if (result.done) throw new Error("Bun watcher closed before observing the file");
if (result.value.paths.includes(watchedFile)) {
return result.value;
}
}
})();
const timeout = new Promise<never>((_, reject) => {
timeoutId = setTimeout(
() => reject(new Error("Bun filesystem watcher integration timed out")),
5_000,
);
});

await writeFile(watchedFile, "created");
const event = await Promise.race([observed, timeout]);
assertEquals(
event.value?.paths.some((path: string) => path.endsWith("file.txt")),
event.paths.includes(watchedFile),
true,
);

watcher.close();
assertExists(watcher.done);
await watcher.done;
} finally {
if (timeoutId !== undefined) clearTimeout(timeoutId);
watcher?.close();
await watcher?.done;
await rm(root, { recursive: true, force: true });
Expand Down