Skip to content
Merged
Changes from 4 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
180 changes: 177 additions & 3 deletions packages/web-shell/client/hooks/useSessionArtifacts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
interface Deferred<T> {
promise: Promise<T>;
resolve: (value: T) => void;
reject: (reason?: unknown) => void;
}

const sdkMock = vi.hoisted(() => ({
Expand Down Expand Up @@ -53,11 +54,15 @@ let latestState: SessionArtifactsState | undefined;

function deferred<T>(): Deferred<T> {
let resolve: ((value: T) => void) | undefined;
const promise = new Promise<T>((done) => {
let reject: ((reason?: unknown) => void) | undefined;
const promise = new Promise<T>((done, fail) => {
resolve = done;
reject = fail;
});
if (!resolve) throw new Error('deferred promise did not initialize');
return { promise, resolve };
if (!resolve || !reject) {
throw new Error('deferred promise did not initialize');
}
return { promise, resolve, reject };
}

function artifact(id: string): DaemonSessionArtifact {
Expand Down Expand Up @@ -215,4 +220,173 @@ describe('useSessionArtifacts', () => {
'replacement',
]);
});

it('keeps automatic refresh failures silent and recovers on the next refresh (#7427)', async () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
// A transient `Failed to fetch` on an automatic refresh is noise the user
// cannot act on. The panel keeps last-good artifacts when it has them,
// clears `loading`, and exposes no error state.
//
// Every mocked load is a deferred settled explicitly inside `act` (and
// awaited there), the same shape the pre-existing tests in this file
// use: flushing with a bare microtask left the hook's refresh
// continuation off the commit under full-suite parallel load, making
// the last-good assertions intermittently see `artifacts === []`
// (#7427 review).
const load1 = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const load2 = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const load3 = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const load4 = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const load5 = deferred<{ artifacts: DaemonSessionArtifact[] }>();
sdkMock.actions.loadArtifacts
.mockReturnValueOnce(load1.promise)
.mockReturnValueOnce(load2.promise)
.mockReturnValueOnce(load3.promise)
.mockReturnValueOnce(load4.promise)
.mockReturnValueOnce(load5.promise);

await renderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(1);

// Automatic refresh #1: initial mount fails before any last-good data exists.
await act(async () => {
load1.reject(new Error('Failed to fetch'));
await load1.promise.catch(() => undefined);
});
expect(latestState?.loading).toBe(false);
expect(latestState?.error).toBeNull();
expect(latestState?.artifacts).toEqual([]);

// Automatic refresh #2: artifactsVersion recovers and establishes last-good.
sdkMock.artifactsVersion = 1;
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(2);
await act(async () => {
load2.resolve({ artifacts: [artifact('current-artifact')] });
await load2.promise;
});
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'current-artifact',
]);
expect(latestState?.loading).toBe(false);

// Automatic refresh #3: prompt settling back to idle fails transiently.
sdkMock.promptStatus = 'streaming';
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(2);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
sdkMock.promptStatus = 'idle';
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(3);
await act(async () => {
load3.reject(new Error('Failed to fetch'));
await load3.promise.catch(() => undefined);
});
expect(latestState?.loading).toBe(false);
expect(latestState?.error).toBeNull();
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'current-artifact',
]);

// Automatic refresh #4: artifactsVersion fails and still keeps last-good.
sdkMock.artifactsVersion = 2;
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(4);
await act(async () => {
load4.reject(new Error('Failed to fetch'));
await load4.promise.catch(() => undefined);
});
expect(latestState?.loading).toBe(false);
expect(latestState?.error).toBeNull();
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'current-artifact',
]);

// Re-rendering the same version is not a new automatic refresh.
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(4);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

// The panel is not wedged: the next automatic refresh recovers.
sdkMock.artifactsVersion = 3;
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(5);
await act(async () => {
load5.resolve({ artifacts: [artifact('recovered-artifact')] });
await load5.promise;
});
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'recovered-artifact',
]);
});

it('ignores loading cleanup from superseded artifact refresh failures', async () => {
const initialLoad = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const staleLoad = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const finalLoad = deferred<{ artifacts: DaemonSessionArtifact[] }>();
sdkMock.actions.loadArtifacts
.mockReturnValueOnce(initialLoad.promise)
.mockReturnValueOnce(staleLoad.promise)
.mockReturnValueOnce(finalLoad.promise);

await renderHookHost();
await act(async () => {
initialLoad.resolve({ artifacts: [artifact('current-artifact')] });
await initialLoad.promise;
});

sdkMock.artifactsVersion = 1;
await rerenderHookHost();
expect(latestState?.loading).toBe(true);

sdkMock.artifactsVersion = 2;
await rerenderHookHost();
// The stale failure lands while the superseding load is still in
// flight — the requestId guard must keep its cleanup from clearing
// `loading` prematurely (R2-2, #7427 review).
await act(async () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
staleLoad.reject(new Error('Failed to fetch'));
await staleLoad.promise.catch(() => undefined);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
});
expect(latestState?.loading).toBe(true);
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'current-artifact',
]);

await act(async () => {
finalLoad.resolve({ artifacts: [artifact('replacement')] });
await finalLoad.promise;
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
});
expect(latestState?.loading).toBe(false);
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'replacement',
]);
});

it('refreshes when the prompt settles from waiting to idle (#7427)', async () => {
// `'waiting'` is a real prompt status (queued prompt / observer text
// deltas before any generation signal); the settling trigger must not
// be specialized to `'streaming'` (#7427 review).
const initialLoad = deferred<{ artifacts: DaemonSessionArtifact[] }>();
const settleLoad = deferred<{ artifacts: DaemonSessionArtifact[] }>();
sdkMock.actions.loadArtifacts
.mockReturnValueOnce(initialLoad.promise)
.mockReturnValueOnce(settleLoad.promise);
sdkMock.promptStatus = 'waiting';

await renderHookHost();
await act(async () => {
initialLoad.resolve({ artifacts: [artifact('current-artifact')] });
await initialLoad.promise;
});
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(1);

sdkMock.promptStatus = 'idle';
await rerenderHookHost();
expect(sdkMock.actions.loadArtifacts).toHaveBeenCalledTimes(2);
await act(async () => {
settleLoad.resolve({ artifacts: [artifact('settled-artifact')] });
await settleLoad.promise;
});
expect(latestState?.artifacts.map((item) => item.id)).toEqual([
'settled-artifact',
]);
});
});
Loading