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:
parent
2af44035b8
commit
a199f1daf1
|
|
@ -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')
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue