|
| 1 | +import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Minimal PrivateProfile shape from specs/behaviors/private-storage.md. |
| 7 | + * The authoritative Zod schema lives in packages/shared once storage-foundation |
| 8 | + * lands; this is the structural subset the test helper needs to compile. |
| 9 | + */ |
| 10 | +export interface PrivateProfile { |
| 11 | + readonly personId: string; |
| 12 | + readonly email: string; |
| 13 | + readonly emailRefreshedAt: string; |
| 14 | + readonly newsletter: { |
| 15 | + readonly optedIn: boolean; |
| 16 | + readonly optedInAt: string | null; |
| 17 | + readonly unsubscribeToken: string | null; |
| 18 | + }; |
| 19 | + readonly updatedAt: string; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Subset of the PrivateStore interface (specs/behaviors/private-storage.md). |
| 24 | + * Implements only the methods exercised by test helpers today; the full |
| 25 | + * interface lands with storage-foundation. |
| 26 | + */ |
| 27 | +export interface TestPrivateStore { |
| 28 | + putProfile(profile: PrivateProfile): Promise<void>; |
| 29 | + getProfile(personId: string): Promise<PrivateProfile | null>; |
| 30 | + findPersonIdByEmail(email: string): Promise<string | null>; |
| 31 | +} |
| 32 | + |
| 33 | +export interface AppTestPrivateStore { |
| 34 | + readonly store: TestPrivateStore; |
| 35 | + /** Absolute path to the temp directory backing this store. */ |
| 36 | + readonly path: string; |
| 37 | + /** Remove the temp directory. Idempotent. */ |
| 38 | + readonly cleanup: () => Promise<void>; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Create a filesystem-backed PrivateStore fixture in a temp directory. |
| 43 | + * |
| 44 | + * Writes are atomic via temp-file-then-rename, matching the production |
| 45 | + * filesystem backend contract in specs/behaviors/private-storage.md. |
| 46 | + * |
| 47 | + * This shim implements the test-facing surface only. The production |
| 48 | + * filesystem and S3 backends land with the storage-foundation plan. |
| 49 | + */ |
| 50 | +export async function createTestPrivateStore(): Promise<AppTestPrivateStore> { |
| 51 | + const dir = await mkdtemp(join(tmpdir(), 'cfp-private-store-')); |
| 52 | + await mkdir(dir, { recursive: true }); |
| 53 | + |
| 54 | + const profilesPath = join(dir, 'profiles.jsonl'); |
| 55 | + |
| 56 | + const readProfiles = async (): Promise<Map<string, PrivateProfile>> => { |
| 57 | + const map = new Map<string, PrivateProfile>(); |
| 58 | + let raw: string; |
| 59 | + try { |
| 60 | + raw = await readFile(profilesPath, 'utf8'); |
| 61 | + } catch { |
| 62 | + return map; |
| 63 | + } |
| 64 | + for (const line of raw.split('\n')) { |
| 65 | + const trimmed = line.trim(); |
| 66 | + if (!trimmed) continue; |
| 67 | + const record = JSON.parse(trimmed) as PrivateProfile; |
| 68 | + map.set(record.personId, record); |
| 69 | + } |
| 70 | + return map; |
| 71 | + }; |
| 72 | + |
| 73 | + const writeProfiles = async (profiles: Map<string, PrivateProfile>): Promise<void> => { |
| 74 | + const lines = [...profiles.values()].map((p) => JSON.stringify(p)).join('\n'); |
| 75 | + const tmp = `${profilesPath}.tmp`; |
| 76 | + await writeFile(tmp, lines ? lines + '\n' : '', 'utf8'); |
| 77 | + await rename(tmp, profilesPath); |
| 78 | + }; |
| 79 | + |
| 80 | + const store: TestPrivateStore = { |
| 81 | + async putProfile(profile) { |
| 82 | + const profiles = await readProfiles(); |
| 83 | + profiles.set(profile.personId, profile); |
| 84 | + await writeProfiles(profiles); |
| 85 | + }, |
| 86 | + |
| 87 | + async getProfile(personId) { |
| 88 | + const profiles = await readProfiles(); |
| 89 | + return profiles.get(personId) ?? null; |
| 90 | + }, |
| 91 | + |
| 92 | + async findPersonIdByEmail(email) { |
| 93 | + const profiles = await readProfiles(); |
| 94 | + for (const profile of profiles.values()) { |
| 95 | + if (profile.email.toLowerCase() === email.toLowerCase()) { |
| 96 | + return profile.personId; |
| 97 | + } |
| 98 | + } |
| 99 | + return null; |
| 100 | + }, |
| 101 | + }; |
| 102 | + |
| 103 | + let cleaned = false; |
| 104 | + return { |
| 105 | + store, |
| 106 | + path: dir, |
| 107 | + cleanup: async () => { |
| 108 | + if (cleaned) return; |
| 109 | + cleaned = true; |
| 110 | + await rm(dir, { recursive: true, force: true }); |
| 111 | + }, |
| 112 | + }; |
| 113 | +} |
0 commit comments