mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-08-12 13:55:51 -07:00
feat(stats): speed up session maintenance and improve stats UI (#111)
This commit is contained in:
@@ -151,6 +151,56 @@ test('syncYomitanDefaultAnkiServer injects force override when enabled', async (
|
||||
assert.match(scriptValue, /forceOverride = true/);
|
||||
});
|
||||
|
||||
test('syncYomitanDefaultAnkiServer updates the active profile Anki deck', async () => {
|
||||
const optionsFull = {
|
||||
profileCurrent: 0,
|
||||
profiles: [
|
||||
{
|
||||
options: {
|
||||
anki: {
|
||||
server: 'http://127.0.0.1:8766',
|
||||
cardFormats: [
|
||||
{ type: 'term', deck: 'Default', model: 'Mining Note', fields: {} },
|
||||
{ type: 'kanji', deck: 'Kanji', model: 'Kanji Note', fields: {} },
|
||||
],
|
||||
terms: { deck: 'Default', model: 'Legacy Note', fields: {} },
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
let savedOptions: typeof optionsFull | null = null;
|
||||
const deps = createDeps((script) =>
|
||||
runInjectedYomitanScript(script, (action, params) => {
|
||||
if (action === 'optionsGetFull') {
|
||||
return JSON.parse(JSON.stringify(optionsFull));
|
||||
}
|
||||
if (action === 'setAllSettings') {
|
||||
savedOptions = (params as { value: typeof optionsFull }).value;
|
||||
return true;
|
||||
}
|
||||
throw new Error(`Unexpected action: ${action}`);
|
||||
}),
|
||||
);
|
||||
|
||||
const synced = await syncYomitanDefaultAnkiServer(
|
||||
'http://127.0.0.1:8766',
|
||||
deps,
|
||||
{
|
||||
error: () => undefined,
|
||||
info: () => undefined,
|
||||
},
|
||||
{ deck: 'Minecraft', forceOverride: true },
|
||||
);
|
||||
|
||||
assert.equal(synced, true);
|
||||
assert.ok(savedOptions);
|
||||
const saved = savedOptions as typeof optionsFull;
|
||||
assert.equal(saved.profiles[0]?.options.anki.cardFormats[0]?.deck, 'Minecraft');
|
||||
assert.equal(saved.profiles[0]?.options.anki.cardFormats[1]?.deck, 'Kanji');
|
||||
assert.equal(saved.profiles[0]?.options.anki.terms.deck, 'Minecraft');
|
||||
});
|
||||
|
||||
test('syncYomitanDefaultAnkiServer logs and returns false on script failure', async () => {
|
||||
const deps = createDeps(async () => {
|
||||
throw new Error('execute failed');
|
||||
|
||||
@@ -1783,6 +1783,7 @@ export async function syncYomitanDefaultAnkiServer(
|
||||
logger: LoggerLike,
|
||||
options?: {
|
||||
forceOverride?: boolean;
|
||||
deck?: string;
|
||||
},
|
||||
): Promise<boolean> {
|
||||
const normalizedTargetServer = serverUrl.trim();
|
||||
@@ -1790,6 +1791,7 @@ export async function syncYomitanDefaultAnkiServer(
|
||||
return false;
|
||||
}
|
||||
const forceOverride = options?.forceOverride === true;
|
||||
const normalizedTargetDeck = options?.deck?.trim() ?? '';
|
||||
|
||||
const isReady = await ensureYomitanParserWindow(deps, logger);
|
||||
const parserWindow = deps.getYomitanParserWindow();
|
||||
@@ -1819,6 +1821,7 @@ export async function syncYomitanDefaultAnkiServer(
|
||||
});
|
||||
|
||||
const targetServer = ${JSON.stringify(normalizedTargetServer)};
|
||||
const targetDeck = ${JSON.stringify(normalizedTargetDeck)};
|
||||
const forceOverride = ${forceOverride ? 'true' : 'false'};
|
||||
const optionsFull = await invoke("optionsGetFull", undefined);
|
||||
const profiles = Array.isArray(optionsFull.profiles) ? optionsFull.profiles : [];
|
||||
@@ -1843,18 +1846,54 @@ export async function syncYomitanDefaultAnkiServer(
|
||||
|
||||
const currentServerRaw = targetProfile.options.anki.server;
|
||||
const currentServer = typeof currentServerRaw === "string" ? currentServerRaw.trim() : "";
|
||||
if (currentServer === targetServer) {
|
||||
return { updated: false, matched: true, reason: "already-target", currentServer, targetServer };
|
||||
}
|
||||
const canReplaceCurrent =
|
||||
forceOverride || currentServer.length === 0 || currentServer === "http://127.0.0.1:8765";
|
||||
if (!canReplaceCurrent) {
|
||||
return { updated: false, matched: false, reason: "blocked-existing-server", currentServer, targetServer };
|
||||
let changed = false;
|
||||
if (currentServer !== targetServer) {
|
||||
const canReplaceCurrent =
|
||||
forceOverride || currentServer.length === 0 || currentServer === "http://127.0.0.1:8765";
|
||||
if (!canReplaceCurrent) {
|
||||
return { updated: false, matched: false, reason: "blocked-existing-server", currentServer, targetServer };
|
||||
}
|
||||
|
||||
targetProfile.options.anki.server = targetServer;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (targetDeck) {
|
||||
const cardFormats = Array.isArray(targetProfile.options.anki.cardFormats)
|
||||
? targetProfile.options.anki.cardFormats
|
||||
: [];
|
||||
for (const cardFormat of cardFormats) {
|
||||
if (
|
||||
!cardFormat ||
|
||||
typeof cardFormat !== "object" ||
|
||||
cardFormat.type !== "term" ||
|
||||
cardFormat.enabled === false
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const currentDeck = typeof cardFormat.deck === "string" ? cardFormat.deck.trim() : "";
|
||||
if (currentDeck !== targetDeck) {
|
||||
cardFormat.deck = targetDeck;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
const terms = targetProfile.options.anki.terms;
|
||||
if (terms && typeof terms === "object") {
|
||||
const currentTermDeck = typeof terms.deck === "string" ? terms.deck.trim() : "";
|
||||
if (currentTermDeck !== targetDeck) {
|
||||
terms.deck = targetDeck;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!changed) {
|
||||
return { updated: false, matched: true, reason: "already-target", currentServer, targetServer, targetDeck };
|
||||
}
|
||||
|
||||
targetProfile.options.anki.server = targetServer;
|
||||
await invoke("setAllSettings", { value: optionsFull, source: "subminer" });
|
||||
return { updated: true, matched: true, currentServer, targetServer };
|
||||
return { updated: true, matched: true, currentServer, targetServer, targetDeck };
|
||||
})();
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user