Persist stats exclusions in DB and fix word metrics filtering (#60)

This commit is contained in:
2026-05-03 20:06:13 -07:00
committed by GitHub
parent db30c61327
commit 0915b23dc8
33 changed files with 1890 additions and 208 deletions
+160
View File
@@ -0,0 +1,160 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
getExcludedWordsSnapshot,
initializeExcludedWordsStore,
resetExcludedWordsStoreForTests,
setExcludedWords,
} from './useExcludedWords';
import { BASE_URL } from '../lib/api-client';
const STORAGE_KEY = 'subminer-excluded-words';
function installLocalStorage(initial: Record<string, string> = {}) {
const previous = Object.getOwnPropertyDescriptor(globalThis, 'localStorage');
const values = new Map(Object.entries(initial));
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => values.set(key, value),
removeItem: (key: string) => values.delete(key),
},
});
return {
values,
restore: () => {
if (previous) {
Object.defineProperty(globalThis, 'localStorage', previous);
} else {
delete (globalThis as { localStorage?: unknown }).localStorage;
}
},
};
}
test('initializeExcludedWordsStore seeds empty database exclusions from localStorage', async () => {
resetExcludedWordsStoreForTests();
const localRows = [{ headword: '猫', word: '猫', reading: 'ねこ' }];
const { values: storage, restore } = installLocalStorage({
[STORAGE_KEY]: JSON.stringify(localRows),
});
const originalFetch = globalThis.fetch;
const requests: Array<{ url: string; method: string; body: string }> = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
requests.push({
url: String(input),
method: init?.method ?? 'GET',
body: String(init?.body ?? ''),
});
if (!init?.method) {
return new Response(JSON.stringify([]), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
return new Response(JSON.stringify({ ok: true }), { status: 200 });
}) as typeof globalThis.fetch;
try {
await initializeExcludedWordsStore();
assert.deepEqual(getExcludedWordsSnapshot(), localRows);
assert.deepEqual(requests, [
{ url: `${BASE_URL}/api/stats/excluded-words`, method: 'GET', body: '' },
{
url: `${BASE_URL}/api/stats/excluded-words`,
method: 'PUT',
body: JSON.stringify({ words: localRows }),
},
]);
assert.equal(storage.get(STORAGE_KEY), JSON.stringify(localRows));
} finally {
globalThis.fetch = originalFetch;
restore();
resetExcludedWordsStoreForTests();
}
});
test('setExcludedWords updates the database-backed exclusion list', async () => {
resetExcludedWordsStoreForTests();
const { values: storage, restore } = installLocalStorage();
const originalFetch = globalThis.fetch;
let seenBody = '';
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
seenBody = String(init?.body ?? '');
return new Response(JSON.stringify({ ok: true }), { status: 200 });
}) as typeof globalThis.fetch;
try {
const rows = [{ headword: 'する', word: 'する', reading: 'する' }];
await setExcludedWords(rows);
assert.deepEqual(getExcludedWordsSnapshot(), rows);
assert.equal(seenBody, JSON.stringify({ words: rows }));
assert.equal(storage.get(STORAGE_KEY), JSON.stringify(rows));
} finally {
globalThis.fetch = originalFetch;
restore();
resetExcludedWordsStoreForTests();
}
});
test('setExcludedWords rolls back local state when persistence fails', async () => {
resetExcludedWordsStoreForTests();
const previousRows = [{ headword: '猫', word: '猫', reading: 'ねこ' }];
const nextRows = [{ headword: 'する', word: 'する', reading: 'する' }];
const { values: storage, restore } = installLocalStorage({
[STORAGE_KEY]: JSON.stringify(previousRows),
});
const originalFetch = globalThis.fetch;
const originalConsoleError = console.error;
console.error = () => {};
globalThis.fetch = (async () => {
return new Response('failed', { status: 500 });
}) as typeof globalThis.fetch;
try {
await assert.rejects(() => setExcludedWords(nextRows), /Stats API error: 500/);
assert.deepEqual(getExcludedWordsSnapshot(), previousRows);
assert.equal(storage.get(STORAGE_KEY), JSON.stringify(previousRows));
} finally {
globalThis.fetch = originalFetch;
console.error = originalConsoleError;
restore();
resetExcludedWordsStoreForTests();
}
});
test('initializeExcludedWordsStore retries after transient database load failures', async () => {
resetExcludedWordsStoreForTests();
const { restore } = installLocalStorage();
const originalFetch = globalThis.fetch;
const originalConsoleError = console.error;
console.error = () => {};
let calls = 0;
globalThis.fetch = (async () => {
calls += 1;
if (calls === 1) {
return new Response('failed', { status: 500 });
}
return new Response(JSON.stringify([{ headword: '猫', word: '猫', reading: 'ねこ' }]), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}) as typeof globalThis.fetch;
try {
await initializeExcludedWordsStore();
await initializeExcludedWordsStore();
assert.equal(calls, 2);
assert.deepEqual(getExcludedWordsSnapshot(), [{ headword: '猫', word: '猫', reading: 'ねこ' }]);
} finally {
globalThis.fetch = originalFetch;
console.error = originalConsoleError;
restore();
resetExcludedWordsStoreForTests();
}
});
+106 -20
View File
@@ -1,10 +1,8 @@
import { useCallback, useSyncExternalStore } from 'react';
import { useCallback, useEffect, useSyncExternalStore } from 'react';
import { apiClient } from '../lib/api-client';
import type { StatsExcludedWord } from '../types/stats';
export interface ExcludedWord {
headword: string;
word: string;
reading: string;
}
export type ExcludedWord = StatsExcludedWord;
const STORAGE_KEY = 'subminer-excluded-words';
@@ -14,16 +12,37 @@ function toKey(w: ExcludedWord): string {
let cached: ExcludedWord[] | null = null;
let cachedKeys: Set<string> | null = null;
let initialized: Promise<void> | null = null;
let revision = 0;
const listeners = new Set<() => void>();
function readLocalStorage(): ExcludedWord[] {
if (typeof localStorage === 'undefined') return [];
try {
const raw = localStorage.getItem(STORAGE_KEY);
const parsed: unknown = raw ? JSON.parse(raw) : [];
if (!Array.isArray(parsed)) return [];
return parsed.filter(
(row): row is ExcludedWord =>
row !== null &&
typeof row === 'object' &&
typeof (row as ExcludedWord).headword === 'string' &&
typeof (row as ExcludedWord).word === 'string' &&
typeof (row as ExcludedWord).reading === 'string',
);
} catch {
return [];
}
}
function writeLocalStorage(words: ExcludedWord[]): void {
if (typeof localStorage === 'undefined') return;
localStorage.setItem(STORAGE_KEY, JSON.stringify(words));
}
function load(): ExcludedWord[] {
if (cached) return cached;
try {
const raw = localStorage.getItem(STORAGE_KEY);
cached = raw ? JSON.parse(raw) : [];
} catch {
cached = [];
}
cached = readLocalStorage();
return cached!;
}
@@ -33,24 +52,89 @@ function getKeySet(): Set<string> {
return cachedKeys;
}
function persist(words: ExcludedWord[]) {
function applyWords(words: ExcludedWord[]): void {
cached = words;
cachedKeys = new Set(words.map(toKey));
localStorage.setItem(STORAGE_KEY, JSON.stringify(words));
writeLocalStorage(words);
for (const fn of listeners) fn();
}
function getSnapshot(): ExcludedWord[] {
export function getExcludedWordsSnapshot(): ExcludedWord[] {
return load();
}
export async function setExcludedWords(words: ExcludedWord[]): Promise<void> {
const previousWords = [...load()];
const previousRevision = revision;
const writeRevision = previousRevision + 1;
revision = writeRevision;
applyWords(words);
try {
await apiClient.setExcludedWords(words);
} catch (error) {
if (revision === writeRevision) {
revision = previousRevision;
applyWords(previousWords);
}
console.error('Failed to persist excluded words to stats database', error);
throw error;
}
}
export function initializeExcludedWordsStore(): Promise<void> {
if (initialized) return initialized;
const startRevision = revision;
initialized = (async () => {
const localWords = load();
let dbWords: ExcludedWord[];
try {
dbWords = await apiClient.getExcludedWords();
} catch (error) {
initialized = null;
console.error('Failed to load excluded words from stats database', error);
return;
}
if (revision !== startRevision) {
initialized = null;
return;
}
if (dbWords.length > 0) {
applyWords(dbWords);
return;
}
if (localWords.length > 0) {
try {
await setExcludedWords(localWords);
} catch {
initialized = null;
}
return;
}
applyWords([]);
})();
return initialized;
}
export function resetExcludedWordsStoreForTests(): void {
cached = null;
cachedKeys = null;
initialized = null;
revision = 0;
listeners.clear();
}
function subscribe(fn: () => void): () => void {
listeners.add(fn);
return () => listeners.delete(fn);
}
export function useExcludedWords() {
const excluded = useSyncExternalStore(subscribe, getSnapshot);
const excluded = useSyncExternalStore(subscribe, getExcludedWordsSnapshot);
useEffect(() => {
void initializeExcludedWordsStore();
}, []);
const isExcluded = useCallback(
(w: { headword: string; word: string; reading: string }) => getKeySet().has(toKey(w)),
@@ -61,17 +145,19 @@ export function useExcludedWords() {
const key = toKey(w);
const current = load();
if (getKeySet().has(key)) {
persist(current.filter((e) => toKey(e) !== key));
void setExcludedWords(current.filter((e) => toKey(e) !== key));
} else {
persist([...current, w]);
void setExcludedWords([...current, w]);
}
}, []);
const removeExclusion = useCallback((w: ExcludedWord) => {
persist(load().filter((e) => toKey(e) !== toKey(w)));
void setExcludedWords(load().filter((e) => toKey(e) !== toKey(w)));
}, []);
const clearAll = useCallback(() => persist([]), []);
const clearAll = useCallback(() => {
void setExcludedWords([]);
}, []);
return { excluded, isExcluded, toggleExclusion, removeExclusion, clearAll };
}
+1
View File
@@ -42,6 +42,7 @@ export function useSessions(limit = 50) {
export interface KnownWordsTimelinePoint {
linesSeen: number;
knownWordsSeen: number;
totalWordsSeen: number;
}
export function useSessionDetail(sessionId: number | null) {