From a199f1daf104eee9146870dfcda27470c5c7dee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Lohrer?= Date: Tue, 28 Apr 2026 08:11:36 +0200 Subject: [PATCH] feat(snapshot): cache-state fuer last-known-good Co-Authored-By: Claude Opus 4.7 (1M context) --- snapshot/src/core/cache.ts | 18 ++++++++++++++++++ snapshot/tests/cache.test.ts | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 snapshot/src/core/cache.ts create mode 100644 snapshot/tests/cache.test.ts diff --git a/snapshot/src/core/cache.ts b/snapshot/src/core/cache.ts new file mode 100644 index 0000000..c3c315d --- /dev/null +++ b/snapshot/src/core/cache.ts @@ -0,0 +1,18 @@ +export interface CacheState { + lastKnownGoodCount: number + deletedCoords: string[] +} + +export async function readCache(path: string): Promise { + 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 { + await Deno.writeTextFile(path, JSON.stringify(state, null, 2) + '\n') +} diff --git a/snapshot/tests/cache.test.ts b/snapshot/tests/cache.test.ts new file mode 100644 index 0000000..4b66e17 --- /dev/null +++ b/snapshot/tests/cache.test.ts @@ -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) +})