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) +})