Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions src/content/hooks/useSummarize.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @vitest-environment jsdom
/// <reference types="node" />
import { webcrypto } from 'node:crypto';
import { setImmediate } from 'node:timers/promises';
import { act, createElement } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -115,7 +116,24 @@ async function mount(url = pdfUrl) {
});
return () => current;
}
async function waitForSummaryCacheRead(firstRead: number) {
const get = vi.mocked(chrome.storage.local.get);
await vi.waitFor(async () => {
await act(async () => {});
// Only this mount's summary-cache read proves that async settings hashing
// finished. Earlier diagnostic reads (or a previous mount) do not.
expect(
get.mock.calls
.slice(firstRead)
.some(
([key]) =>
typeof key === 'string' && key.startsWith('summaryCacheV2:') && key.endsWith(pdfUrl)
)
).toBe(true);
});
}
async function mountButton() {
const firstRead = vi.mocked(chrome.storage.local.get).mock.calls.length;
container = document.createElement('div');
document.body.append(container);
const table = document.createElement('table');
Expand All @@ -133,12 +151,35 @@ async function mountButton() {
})
)
);
await vi.waitFor(async () => {
await act(async () => {});
expect(chrome.storage.local.get).toHaveBeenCalled();
});
await waitForSummaryCacheRead(firstRead);
return { row, cell, button: cell.querySelector('button')! };
}
// Existing storage reads must not make either mount report readiness while
// its own settings callback is held. Yield event-loop turns, not a timed sleep.
async function mountWithPendingSettings<T>(mount: () => Promise<T>): Promise<T> {
const readSettings = chrome.storage.sync.get;
const pendingSettings = deferred<void>();
const get = vi.spyOn(chrome.storage.sync, 'get').mockImplementation((keys, callback) => {
if (Array.isArray(keys) && keys.some((key) => key === 'provider')) {
void pendingSettings.promise.then(() => readSettings(keys, callback));
} else {
readSettings(keys, callback);
}
});
let mountReady = false;
const pendingMount = mount().then((mounted) => {
mountReady = true;
return mounted;
});
try {
for (let turn = 0; turn < 10; turn++) await setImmediate();
expect(mountReady).toBe(false);
} finally {
pendingSettings.resolve();
get.mockRestore();
}
return pendingMount;
}
function deferred<T>() {
let resolve!: (value: T) => void;
let reject!: (error: Error) => void;
Expand Down Expand Up @@ -812,7 +853,7 @@ describe('実Reactの要約行アクション配置', () => {
sendMessage.mockResolvedValueOnce(response);
const writeText = vi.fn().mockResolvedValue(undefined);
vi.stubGlobal('navigator', { clipboard: { writeText } });
let mounted = await mountButton();
let mounted = await mountWithPendingSettings(mountButton);
await click(mounted.button);
let summaryRow = await summaryRowFor(mounted.row);
expect(stored[await keyFor('full')]).toMatchObject({
Expand Down Expand Up @@ -1393,24 +1434,23 @@ describe('保存失敗と一覧ライフサイクルの回復', () => {
});
}
async function mountLifecycle() {
const firstRead = vi.mocked(chrome.storage.local.get).mock.calls.length;
container = document.createElement('div');
document.body.append(container);
const fixture = frameFixture();
await act(async () => {
stopContentScript = startContentScript();
});
await vi.waitFor(async () => {
await act(async () => {});
expect(chrome.storage.local.get).toHaveBeenCalled();
});
await waitForSummaryCacheRead(firstRead);
return fixture;
}

it('OFFでReactと購読を破棄し、遅い応答を保存・再表示せず、ONで一組だけ作り直す', async () => {
settings.experimentalScoring = false;
const pending = deferred<unknown>();
sendMessage.mockReturnValueOnce(pending.promise).mockResolvedValue(await responseFor());
const { frame, doc, row } = await mountLifecycle();
await chrome.storage.local.get(SUMMARY_TRACE_KEY);
const { frame, doc, row } = await mountWithPendingSettings(mountLifecycle);
const subscriptions = listeners.size;
await act(async () => {
window.dispatchEvent(new PageTransitionEvent('pagehide', { persisted: true }));
Expand Down
Loading