- Added symlink resolution - Added mount resolution - Fixed filesystem not awaiting for disks to be mounted (now `FileSystem.init()` needs to be called with `await`) - Removed old test fixture files - Added a feature-full `resolvePath`test suite *all tests passing green*
143 lines
4.6 KiB
TypeScript
143 lines
4.6 KiB
TypeScript
// @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<string, unknown>) {
|
|
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<NotADirectoryError>) {
|
|
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<AlreadyExistsError>) {
|
|
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<NodeNotFoundError>) {
|
|
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<IllegalMutationError>) {
|
|
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<DirectoryLoopError>) {
|
|
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<DeviceNotFoundError>) {
|
|
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<NotAFileError>) {
|
|
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<FileSystemInitializationError>,
|
|
) {
|
|
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<InvalidNodeNameError>) {
|
|
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<FileSystemCorruptionError>,
|
|
) {
|
|
return await expectThrows(fn, {
|
|
type: "FileSystemCorruption",
|
|
message: expect.any(String) as string,
|
|
timestamp: expect.any(Number) as number,
|
|
...overrides,
|
|
});
|
|
}
|