import { GenericDomainError, type DomainError } from "../errors/generic-domain-error.js"; import type { ArtifactStore } from "../../domain/ports/artifact-store.port.js"; import { Err, Ok, type Result } from "@tff/core"; export class InMemoryArtifactStore implements ArtifactStore { private files = new Map(); private failOnWritePaths = new Set(); simulateWriteFailure(path: string): void { this.failOnWritePaths.add(path); } async read(path: string): Promise> { const content = this.files.get(path); if (content === undefined) return Err(new GenericDomainError("NOT_FOUND", `File not found: ${path}`, { path })); return Ok(content); } async write(path: string, content: string): Promise> { if (this.failOnWritePaths.has(path)) { return Err( new GenericDomainError("WRITE_FAILURE", `Simulated write failure for: ${path}`, { path }), ); } this.files.set(path, content); return Ok(undefined); } async exists(path: string): Promise { return this.files.has(path); } async list(directory: string): Promise> { const prefix = directory.endsWith("/") ? directory : `${directory}/`; const matches = [...this.files.keys()].filter((k) => k.startsWith(prefix)); return Ok(matches); } async mkdir(_path: string): Promise> { return Ok(undefined); } reset(): void { this.files.clear(); } seed(files: Record): void { for (const [path, content] of Object.entries(files)) { this.files.set(path, content); } } getAll(): Map { return new Map(this.files); } }