From 810f0475edb0ddddf8f23311137fd4cb1d0b4aa2 Mon Sep 17 00:00:00 2001 From: Youzini-afk <13153778771cx@gmail.com> Date: Thu, 23 Apr 2026 02:53:39 +0800 Subject: [PATCH] test: stabilize opfs persistence manifest reads --- tests/opfs-persistence.mjs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/opfs-persistence.mjs b/tests/opfs-persistence.mjs index 184bcf9..3dc69ec 100644 --- a/tests/opfs-persistence.mjs +++ b/tests/opfs-persistence.mjs @@ -152,9 +152,27 @@ function getNestedDirectory(directoryHandle, ...names) { return current; } -function readJsonFromDirectory(directoryHandle, filename) { +async function readJsonFromDirectory(directoryHandle, filename, { retries = 5 } = {}) { assert.ok(directoryHandle.files.has(filename), `文件必须存在: ${filename}`); - return JSON.parse(String(directoryHandle.files.get(filename) || "")); + let lastError = null; + let lastText = ""; + const normalizedRetries = Math.max(0, Math.floor(Number(retries) || 0)); + for (let attempt = 0; attempt <= normalizedRetries; attempt += 1) { + lastText = String(directoryHandle.files.get(filename) || ""); + if (lastText) { + try { + return JSON.parse(lastText); + } catch (error) { + lastError = error; + } + } + if (attempt < normalizedRetries) { + await new Promise((resolve) => setTimeout(resolve, 0)); + } + } + throw new Error( + `读取目录 JSON 失败: ${filename} len=${lastText.length} error=${String(lastError?.message || "empty")}`, + ); } function buildLegacyGraph(chatId) { @@ -288,7 +306,7 @@ async function testImportExportPersistenceAndFileRotation() { }, ); - const manifestAfterFirstImport = readJsonFromDirectory(chatDirectory, "manifest.json"); + const manifestAfterFirstImport = await readJsonFromDirectory(chatDirectory, "manifest.json"); assert.equal(manifestAfterFirstImport.formatVersion, 2); assert.equal(manifestAfterFirstImport.baseRevision, 4); assert.equal(manifestAfterFirstImport.headRevision, 4); @@ -376,7 +394,7 @@ async function testImportExportPersistenceAndFileRotation() { }, ); - const manifestAfterSecondImport = readJsonFromDirectory(chatDirectory, "manifest.json"); + const manifestAfterSecondImport = await readJsonFromDirectory(chatDirectory, "manifest.json"); assert.equal(manifestAfterSecondImport.formatVersion, 2); assert.equal(manifestAfterSecondImport.baseRevision, 6); assert.equal(manifestAfterSecondImport.headRevision, 6);