diff --git a/tests/helpers/memory-opfs.ts b/tests/helpers/memory-opfs.ts new file mode 100644 index 00000000..ec226ffe --- /dev/null +++ b/tests/helpers/memory-opfs.ts @@ -0,0 +1,123 @@ +/** + * An in-memory Origin Private File System for the OPFS worker runtime: just + * the `FileSystemDirectoryHandle` / `FileSystemFileHandle` surface it uses. + * Moved out of `tests/unit/opfs-worker-runtime.test.ts` so the recipe tests + * can run the same runtime (2026-11-03 05-04). + */ + +export type MemoryNode = MemoryDirectory | MemoryFile; + +export class MemoryFile { + readonly kind = "file"; + bytes = new Uint8Array(); + lastModified = 0; + reads = 0; +} + +export class MemoryDirectory { + readonly kind = "directory"; + readonly children = new Map(); + + async getDirectoryHandle( + name: string, + options: FileSystemGetDirectoryOptions = {}, + ): Promise { + const current = this.children.get(name); + if (current instanceof MemoryDirectory) { + return current as unknown as FileSystemDirectoryHandle; + } + if (current || !options.create) throw notFound(); + const created = new MemoryDirectory(); + this.children.set(name, created); + return created as unknown as FileSystemDirectoryHandle; + } + + async getFileHandle( + name: string, + options: FileSystemGetFileOptions = {}, + ): Promise { + const current = this.children.get(name); + if (current instanceof MemoryFile) { + return fileHandle(current); + } + if (current || !options.create) throw notFound(); + const created = new MemoryFile(); + this.children.set(name, created); + return fileHandle(created); + } + + async removeEntry( + name: string, + options: FileSystemRemoveOptions = {}, + ): Promise { + const current = this.children.get(name); + if (!current) throw notFound(); + if ( + current instanceof MemoryDirectory && + current.children.size > 0 && + !options.recursive + ) { + throw new DOMException("Directory is not empty.", "InvalidModificationError"); + } + this.children.delete(name); + } + + async *entries(): AsyncIterableIterator< + [string, FileSystemDirectoryHandle | FileSystemFileHandle] + > { + for (const [name, node] of this.children) { + yield [ + name, + node instanceof MemoryDirectory + ? (node as unknown as FileSystemDirectoryHandle) + : fileHandle(node), + ]; + } + } + + has(path: readonly string[]): boolean { + let current: MemoryNode = this; + for (const segment of path) { + if (!(current instanceof MemoryDirectory)) return false; + const next = current.children.get(segment); + if (!next) return false; + current = next; + } + return true; + } +} + +function fileHandle(file: MemoryFile): FileSystemFileHandle { + return { + kind: "file", + name: "memory", + async getFile() { + file.reads += 1; + const blob = new Blob([Uint8Array.from(file.bytes)]); + Object.defineProperty(blob, "lastModified", { + value: file.lastModified, + }); + return blob as File; + }, + async createWritable() { + let pending = Uint8Array.from(file.bytes); + return { + async write(data: FileSystemWriteChunkType) { + if (!(data instanceof Uint8Array)) { + throw new TypeError("The test writer accepts Uint8Array only."); + } + pending = Uint8Array.from(data); + }, + async close() { + file.bytes = pending; + file.lastModified = Date.now(); + }, + async abort() {}, + } as unknown as FileSystemWritableFileStream; + }, + } as FileSystemFileHandle; +} + +function notFound(): DOMException { + return new DOMException("Entry was not found.", "NotFoundError"); +} diff --git a/tests/unit/opfs-worker-runtime.test.ts b/tests/unit/opfs-worker-runtime.test.ts index a9699b72..9b361ebc 100644 --- a/tests/unit/opfs-worker-runtime.test.ts +++ b/tests/unit/opfs-worker-runtime.test.ts @@ -38,6 +38,11 @@ import { type OpfsWorkerRuntime, } from "../../src/adapters/storage/opfs/opfs-worker-runtime.ts"; import { MemoryIndexedDbFactory } from "../helpers/memory-indexeddb.ts"; +import { + MemoryDirectory, + MemoryFile, + type MemoryNode, +} from "../helpers/memory-opfs.ts"; const scopeA: OpfsStorageScope = Object.freeze({ namespace: "durable-objects", @@ -71,123 +76,6 @@ const runtimePolicy = resolveOpfsRuntimePolicy({ orphanGracePeriodMs: 60_000, }); -type MemoryNode = MemoryDirectory | MemoryFile; - -class MemoryFile { - readonly kind = "file"; - bytes = new Uint8Array(); - lastModified = 0; - reads = 0; -} - -class MemoryDirectory { - readonly kind = "directory"; - readonly children = new Map(); - - async getDirectoryHandle( - name: string, - options: FileSystemGetDirectoryOptions = {}, - ): Promise { - const current = this.children.get(name); - if (current instanceof MemoryDirectory) { - return current as unknown as FileSystemDirectoryHandle; - } - if (current || !options.create) throw notFound(); - const created = new MemoryDirectory(); - this.children.set(name, created); - return created as unknown as FileSystemDirectoryHandle; - } - - async getFileHandle( - name: string, - options: FileSystemGetFileOptions = {}, - ): Promise { - const current = this.children.get(name); - if (current instanceof MemoryFile) { - return fileHandle(current); - } - if (current || !options.create) throw notFound(); - const created = new MemoryFile(); - this.children.set(name, created); - return fileHandle(created); - } - - async removeEntry( - name: string, - options: FileSystemRemoveOptions = {}, - ): Promise { - const current = this.children.get(name); - if (!current) throw notFound(); - if ( - current instanceof MemoryDirectory && - current.children.size > 0 && - !options.recursive - ) { - throw new DOMException("Directory is not empty.", "InvalidModificationError"); - } - this.children.delete(name); - } - - async *entries(): AsyncIterableIterator< - [string, FileSystemDirectoryHandle | FileSystemFileHandle] - > { - for (const [name, node] of this.children) { - yield [ - name, - node instanceof MemoryDirectory - ? (node as unknown as FileSystemDirectoryHandle) - : fileHandle(node), - ]; - } - } - - has(path: readonly string[]): boolean { - let current: MemoryNode = this; - for (const segment of path) { - if (!(current instanceof MemoryDirectory)) return false; - const next = current.children.get(segment); - if (!next) return false; - current = next; - } - return true; - } -} - -function fileHandle(file: MemoryFile): FileSystemFileHandle { - return { - kind: "file", - name: "memory", - async getFile() { - file.reads += 1; - const blob = new Blob([Uint8Array.from(file.bytes)]); - Object.defineProperty(blob, "lastModified", { - value: file.lastModified, - }); - return blob as File; - }, - async createWritable() { - let pending = Uint8Array.from(file.bytes); - return { - async write(data: FileSystemWriteChunkType) { - if (!(data instanceof Uint8Array)) { - throw new TypeError("The test writer accepts Uint8Array only."); - } - pending = Uint8Array.from(data); - }, - async close() { - file.bytes = pending; - file.lastModified = Date.now(); - }, - async abort() {}, - } as unknown as FileSystemWritableFileStream; - }, - } as FileSystemFileHandle; -} - -function notFound(): DOMException { - return new DOMException("Entry was not found.", "NotFoundError"); -} - const PHYSICAL_GENERATION_A = "a".repeat(32) as OpfsPhysicalGenerationId; function beginRequest(