// @core/filesystem/errors.test-util.ts (adjust path to match yours) import type { AlreadyExistsError, DeviceNotFoundError, DirectoryLoopError, FileSystemCorruptionError, FileSystemInitializationError, IllegalMutationError, InvalidNodeNameError, NodeNotFoundError, NotADirectoryError, NotAFileError, } from "@core/filesystem/errors"; import type { NodeType } from "@core/filesystem/node"; import { expect } from "vitest"; export async function expectThrows(fn: () => unknown, expectedShape: Record) { let thrown: unknown; try { await fn(); } catch (e) { thrown = e; } expect(thrown).toBeDefined(); expect(thrown).toMatchObject(expectedShape); } export async function expectNotADirectoryError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "NotADirectory", inode: expect.any(Number) as number, message: expect.any(String) as string, timestamp: expect.any(Number) as number, nodeType: expect.any(Number) as NodeType, ...overrides, }); } export async function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "AlreadyExists", parentInode: expect.any(Number) as number, name: expect.any(String) as string, message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } /** * FIXED: `NodeNotFoundError` has `pointer: NodePointer` (a string), not `inode: number`. * The previous version asserted `inode: expect.any(Number)` and would have failed * against every real thrown `NodeNotFoundError`. */ export async function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "NodeNotFound", pointer: expect.any(String) as string, message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } export async function expectIllegalMutationError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "IllegalMutation", message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } export async function expectDirectoryLoopError(fn: () => void, overrides?: Partial) { return await expectThrows(fn, { type: "DirectoryLoop", descendantInode: expect.any(Number) as number, ancestorInode: expect.any(Number) as number, message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } // ---- NEW helpers below ---- export async function expectDeviceNotFoundError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "DeviceNotFound", device_id: expect.any(Number) as number, message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } export async function expectNotAFileError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "NotAFile", inode: expect.any(Number) as number, message: expect.any(String) as string, timestamp: expect.any(Number) as number, nodeType: expect.any(Number) as NodeType, ...overrides, }); } export async function expectFileSystemInitializationError( fn: () => unknown, overrides?: Partial, ) { return await expectThrows(fn, { type: "FileSystemInitializationError", message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } export async function expectInvalidNodeNameError(fn: () => unknown, overrides?: Partial) { return await expectThrows(fn, { type: "InvalidNodeName", pointer: expect.any(String) as string, attemptedName: expect.any(String) as string, message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); } export async function expectFileSystemCorruptionError( fn: () => unknown, overrides?: Partial, ) { return await expectThrows(fn, { type: "FileSystemCorruption", message: expect.any(String) as string, timestamp: expect.any(Number) as number, ...overrides, }); }