tests / fakeFs.test.ts
import { describe, expect, it } from "vitest";
import { fakeFs } from "./fakeFs";
describe("fakeFs", () => {
const fs = fakeFs({
files: { "/root/a/SKILL.md": "# a", "/root/a/ref/x.md": "x", "/root/b.txt": "b", "/outside/secret": "s" },
symlinks: { "/root/link": "/outside" },
});
it("lists directories, files and symlinks", async () => {
expect(await fs.readdir("/root")).toEqual([
{ name: "a", isDirectory: true, isFile: false },
{ name: "b.txt", isDirectory: false, isFile: true },
{ name: "link", isDirectory: false, isFile: false },
]);
});
it("stats files and dirs, throws ENOENT otherwise", async () => {
expect((await fs.stat("/root/a")).isDirectory).toBe(true);
expect((await fs.stat("/root/a/SKILL.md")).size).toBe(3);
await expect(fs.stat("/root/nope")).rejects.toMatchObject({ code: "ENOENT" });
});
it("resolves symlinks by prefix in realpath", async () => {
expect(await fs.realpath("/root/link/secret")).toBe("/outside/secret");
expect(await fs.realpath("/root/a/ref/x.md")).toBe("/root/a/ref/x.md");
});
it("realpath throws ENOENT when the path through a symlink does not exist", async () => {
await expect(fs.realpath("/root/link/nope")).rejects.toMatchObject({ code: "ENOENT" });
await expect(fs.realpath("/root/nope")).rejects.toMatchObject({ code: "ENOENT" });
});
it("follows a symlinked directory in stat, readdir and readFile", async () => {
const linked = fakeFs({
files: { "/outside/SKILL.md": "# o", "/outside/sub/y.md": "y" },
symlinks: { "/root/link": "/outside" },
});
expect((await linked.stat("/root/link")).isDirectory).toBe(true);
expect((await linked.stat("/root/link/SKILL.md")).size).toBe(3);
expect(await linked.readdir("/root/link")).toEqual([
{ name: "SKILL.md", isDirectory: false, isFile: true },
{ name: "sub", isDirectory: true, isFile: false },
]);
expect((await linked.readFile("/root/link/SKILL.md")).toString()).toBe("# o");
});
it("write bumps mtime", async () => {
const before = (await fs.stat("/root/a/SKILL.md")).mtimeMs;
fs.write("/root/a/SKILL.md", "# a2");
expect((await fs.stat("/root/a/SKILL.md")).mtimeMs).toBeGreaterThan(before);
});
});
tests / fakeFs.test.ts
import { describe, expect, it } from "vitest";
import { fakeFs } from "./fakeFs";
describe("fakeFs", () => {
const fs = fakeFs({
files: { "/root/a/SKILL.md": "# a", "/root/a/ref/x.md": "x", "/root/b.txt": "b", "/outside/secret": "s" },
symlinks: { "/root/link": "/outside" },
});
it("lists directories, files and symlinks", async () => {
expect(await fs.readdir("/root")).toEqual([
{ name: "a", isDirectory: true, isFile: false },
{ name: "b.txt", isDirectory: false, isFile: true },
{ name: "link", isDirectory: false, isFile: false },
]);
});
it("stats files and dirs, throws ENOENT otherwise", async () => {
expect((await fs.stat("/root/a")).isDirectory).toBe(true);
expect((await fs.stat("/root/a/SKILL.md")).size).toBe(3);
await expect(fs.stat("/root/nope")).rejects.toMatchObject({ code: "ENOENT" });
});
it("resolves symlinks by prefix in realpath", async () => {
expect(await fs.realpath("/root/link/secret")).toBe("/outside/secret");
expect(await fs.realpath("/root/a/ref/x.md")).toBe("/root/a/ref/x.md");
});
it("realpath throws ENOENT when the path through a symlink does not exist", async () => {
await expect(fs.realpath("/root/link/nope")).rejects.toMatchObject({ code: "ENOENT" });
await expect(fs.realpath("/root/nope")).rejects.toMatchObject({ code: "ENOENT" });
});
it("follows a symlinked directory in stat, readdir and readFile", async () => {
const linked = fakeFs({
files: { "/outside/SKILL.md": "# o", "/outside/sub/y.md": "y" },
symlinks: { "/root/link": "/outside" },
});
expect((await linked.stat("/root/link")).isDirectory).toBe(true);
expect((await linked.stat("/root/link/SKILL.md")).size).toBe(3);
expect(await linked.readdir("/root/link")).toEqual([
{ name: "SKILL.md", isDirectory: false, isFile: true },
{ name: "sub", isDirectory: true, isFile: false },
]);
expect((await linked.readFile("/root/link/SKILL.md")).toString()).toBe("# o");
});
it("write bumps mtime", async () => {
const before = (await fs.stat("/root/a/SKILL.md")).mtimeMs;
fs.write("/root/a/SKILL.md", "# a2");
expect((await fs.stat("/root/a/SKILL.md")).mtimeMs).toBeGreaterThan(before);
});
});