feat(snapshot): cache-state fuer last-known-good

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jörg Lohrer 2026-04-28 08:11:36 +02:00
parent 2af44035b8
commit a199f1daf1
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,18 @@
export interface CacheState {
lastKnownGoodCount: number
deletedCoords: string[]
}
export async function readCache(path: string): Promise<CacheState | undefined> {
try {
const text = await Deno.readTextFile(path)
return JSON.parse(text) as CacheState
} catch (err) {
if (err instanceof Deno.errors.NotFound) return undefined
throw err
}
}
export async function writeCache(path: string, state: CacheState): Promise<void> {
await Deno.writeTextFile(path, JSON.stringify(state, null, 2) + '\n')
}

View File

@ -0,0 +1,19 @@
import { assertEquals } from '@std/assert'
import { join } from '@std/path'
import { readCache, writeCache, type CacheState } from '../src/core/cache.ts'
Deno.test('readCache: file fehlt -> undefined', async () => {
const dir = await Deno.makeTempDir()
const path = join(dir, 'cache.json')
const cache = await readCache(path)
assertEquals(cache, undefined)
})
Deno.test('writeCache + readCache: round-trip', async () => {
const dir = await Deno.makeTempDir()
const path = join(dir, 'cache.json')
const state: CacheState = { lastKnownGoodCount: 27, deletedCoords: ['30023:P:dead'] }
await writeCache(path, state)
const out = await readCache(path)
assertEquals(out, state)
})