From 8da33a5cf009763588a7d8acf42977022c743f4e Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Wed, 4 Nov 2026 20:40:25 +0900 Subject: [PATCH] =?UTF-8?q?test(storage):=20=EB=A9=94=EB=AA=A8=EB=A6=AC=20?= =?UTF-8?q?OPFS=20=ED=8C=8C=EC=9D=BC=20=EC=8B=9C=EC=8A=A4=ED=85=9C?= =?UTF-8?q?=EC=9D=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20helper=EB=A1=9C=20?= =?UTF-8?q?=EC=98=AE=EA=B8=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: OPFS worker runtime을 메모리에서 돌리는 MemoryDirectory·MemoryFile이 tests/unit/opfs-worker-runtime.test.ts 안에만 있다. 05-04를 고치려면 recipe fake와 runtime에 같은 시나리오를 돌리는 parity 테스트가 tests/recipes에 필요한데, 그 테스트는 이 파일 시스템을 쓸 수 없다. unit 테스트가 recipe를 import하면 optional-recipe 제거 드릴이 깨지므로 parity 테스트를 unit 쪽에 둘 수도 없다. 고른 방법: 그 블록(MemoryNode·MemoryFile·MemoryDirectory·fileHandle· notFound)을 그대로 tests/helpers/memory-opfs.ts로 옮기고 export한다. memory-indexeddb.ts와 같은 자리다. 코드는 바꾸지 않았다. helper는 src를 import하지 않아 browser-file-storage 제거 드릴에서도 남아도 된다. 위험·한계: 동작 변경 없음. 검증: opfs-worker-runtime.test.ts 82건 통과. check:types:test, 바꾼 파일 eslint 통과. Refs: docs/reviews/2026-11-03-maturity-review/05-adapters-storage-kernel.md 05-04 --- tests/helpers/memory-opfs.ts | 123 +++++++++++++++++++++++++ tests/unit/opfs-worker-runtime.test.ts | 122 +----------------------- 2 files changed, 128 insertions(+), 117 deletions(-) create mode 100644 tests/helpers/memory-opfs.ts 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(