fix(assistant): design and page cards keep the newest choice and draw every page they touch

An answer or a refusal no longer overwrites a newer page choice, and Apply
waits for every page selection the chat has sent, also after the card was
closed. A delete of several pages draws each page it takes, the receipt opens
the first page the design wrote through the same rule the strip uses, and the
vacay group's choices wait while a change runs.
This commit is contained in:
Maurice
2026-10-23 01:45:16 +00:00
parent 9e40092507
commit 0095f2105a
16 changed files with 596 additions and 99 deletions
+48 -3
View File
@@ -1,12 +1,13 @@
/**
* FE-AI-STORE-STUDIO-001 to FE-AI-STORE-STUDIO-005
* FE-AI-STORE-STUDIO-001 to FE-AI-STORE-STUDIO-006
*
* A journey's chat hosted by TREK Studio (journey-studio.md 3.1, 4.5): before
* a question or a decision goes out the Studio writes the book's local work,
* and the turn says the Studio draws pages; the page a turn asks for goes to
* the host with the turn's signal; an unhosted chat sends neither; a host
* that ends leaves a newer one in place; agreeing to page images is stored
* for the connection. The repos are faked, nothing reaches Dexie.
* for the connection; a decision waits for what a card of its chat still
* sends (a book design's pages). The repos are faked, nothing reaches Dexie.
*/
import type { AiStatusResponse, AssistantPageRender, UiStreamChunk } from '@trek/shared';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -60,7 +61,7 @@ function fakeHost(order: string[], landed = true) {
}
beforeEach(() => {
useAssistantStore.setState({ status: null, byTrip: {}, studioHosts: {}, lastApplied: null });
useAssistantStore.setState({ status: null, byTrip: {}, studioHosts: {}, decisionHolds: {}, lastApplied: null });
useSettingsStore.setState((s) => ({ settings: { ...s.settings, language: 'en' } }));
});
afterEach(() => vi.restoreAllMocks());
@@ -149,4 +150,48 @@ describe('assistantStore: a chat hosted by the Studio', () => {
// The messages' own acknowledgement is a different one and stays as it was.
expect(store().status?.privacyAcked).toBe(true);
});
it('FE-AI-STORE-STUDIO-006: a decision waits for what a card of its chat still sends; a hold ended or of another chat is not asked', async () => {
const order: string[] = [];
let land: () => void = () => undefined;
const held = vi.fn(() => {
order.push('hold');
return new Promise<void>((resolve) => {
land = () => {
order.push('landed');
resolve();
};
});
});
// A hold whose send fails still lets the decision go on.
const refused = vi.fn(() => Promise.reject(new Error('offline')));
const ended = vi.fn(async () => undefined);
const elsewhere = vi.fn(async () => undefined);
store().holdDecisions(KEY, held);
store().holdDecisions(KEY, refused);
store().holdDecisions(KEY, ended)();
store().holdDecisions(4, elsewhere);
store().hostStudio(KEY, fakeHost(order));
useAssistantStore.setState({ byTrip: { [KEY]: { ...chatOf(store(), KEY), threadId: 9 } } });
const decide = vi.spyOn(assistantRepo, 'decide').mockImplementationOnce(async () => {
order.push('decide');
return streamOf([{ type: 'finish', finishReason: 'stop' }]);
});
const deciding = store().decide(KEY, [{ toolCallId: WRITE_ID, approved: true }]);
// The hold is asked at once, and nothing goes out while what it sends is on its way.
expect(held).toHaveBeenCalledTimes(1);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(decide).not.toHaveBeenCalled();
expect(chatOf(store(), KEY).isRunning).toBe(true);
land();
await deciding;
expect(order).toEqual(['hold', 'landed', 'flush', 'decide']);
expect(refused).toHaveBeenCalledTimes(1);
expect(ended).not.toHaveBeenCalled();
expect(elsewhere).not.toHaveBeenCalled();
expect(chatOf(store(), KEY).error).toBeNull();
});
});