From fe89c8b2043e6b45e33c042a889c9f43cd51e6f7 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Sun, 3 May 2026 15:30:22 +0000 Subject: [PATCH] test: protect manual Luker cache rebuild Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- tests/graph-persistence.mjs | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/graph-persistence.mjs b/tests/graph-persistence.mjs index e19dd98..b94661e 100644 --- a/tests/graph-persistence.mjs +++ b/tests/graph-persistence.mjs @@ -159,6 +159,10 @@ const messageSnippet = extractSnippet( 'function onMessageReceived(messageId = null, type = "") {', "async function onViewGraph() {", ); +const lukerCacheActionSnippet = extractSnippet( + "async function onRebuildLocalCacheFromLukerSidecar() {", + "async function onRepairLukerSidecar() {", +); function createSessionStorage(seed = null) { const store = seed instanceof Map ? seed : new Map(); @@ -1555,6 +1559,7 @@ async function createGraphPersistenceHarness({ persistencePrelude, persistenceCore, messageSnippet, + lukerCacheActionSnippet, ` result = { GRAPH_LOAD_STATES, @@ -1576,6 +1581,7 @@ result = { maybeFlushQueuedGraphPersist, retryPendingGraphPersist, persistExtractionBatchResult, + onRebuildLocalCacheFromLukerSidecar, saveGraphToIndexedDb, cloneGraphForPersistence, assertRecoveryChatStillActive, @@ -4651,6 +4657,60 @@ result = { ); } +{ + const chatId = "chat-luker-manual-cache-rebuild"; + const harness = await createGraphPersistenceHarness({ + chatId, + globalChatId: chatId, + characterId: "char-luker-manual-cache-rebuild", + chatMetadata: { + integrity: "meta-luker-manual-cache-rebuild", + }, + }); + harness.runtimeContext.Luker = { + getContext() { + return harness.runtimeContext.__chatContext; + }, + }; + const graph = stampPersistedGraph( + createMeaningfulGraph(chatId, "luker-manual-cache"), + { + revision: 10, + integrity: "meta-luker-manual-cache-rebuild", + chatId, + reason: "luker-manual-cache-seed", + }, + ); + harness.api.setCurrentGraph(graph); + const persistResult = await harness.api.persistExtractionBatchResult({ + reason: "luker-manual-cache-persist", + lastProcessedAssistantFloor: 6, + }); + await new Promise((resolve) => setTimeout(resolve, 0)); + assert.equal(persistResult.cacheTier, "none"); + assert.equal( + Number(harness.api.getIndexedDbSnapshot()?.meta?.revision || 0), + 0, + "Luker 默认路径不应自动重建浏览器缓存", + ); + + const rebuildResult = await harness.api.onRebuildLocalCacheFromLukerSidecar(); + assert.equal(rebuildResult.handledToast, true); + assert.equal(rebuildResult.result?.loaded, true); + await new Promise((resolve) => setTimeout(resolve, 0)); + await new Promise((resolve) => setTimeout(resolve, 0)); + assert.equal( + Number(harness.api.getIndexedDbSnapshot()?.meta?.revision || 0), + persistResult.revision, + "只有手动重建本地缓存时才应写入浏览器缓存 revision", + ); + assert.equal( + Number(harness.api.getIndexedDbSnapshot()?.nodes?.length || 0), + 1, + "只有手动重建本地缓存时才应写入浏览器缓存 nodes", + ); +} + { const harness = await createGraphPersistenceHarness({ chatId: "chat-luker-v2-load",