From 2cc4f385380d664e5364ea3fcc5d12d0a6133d22 Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 17 Nov 2026 12:28:47 +0800 Subject: [PATCH 1/2] fix(ui): ignore an empty server echo while a card pile is still local sharedState.get returns the empty initial value before the server reply. Applying that reply after start() built a pile cleared it, and the next save persisted the empty pile. --- ui/composables/useCardsMode.ts | 46 ++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/ui/composables/useCardsMode.ts b/ui/composables/useCardsMode.ts index aa30a3a4..187f0cf5 100644 --- a/ui/composables/useCardsMode.ts +++ b/ui/composables/useCardsMode.ts @@ -1,4 +1,4 @@ -import { computed, ref } from 'vue' +import { computed, ref, toRaw } from 'vue' import { useRoute, useRouter } from '#imports' import type { CardRef, @@ -79,18 +79,18 @@ let saveFn: (() => void) | null = null * reply to the initial `get`). Falls back to a timeout so a server that * has nothing saved still releases the hydrate spinner. */ -function waitForFirstUpdate(state: SharedState, timeoutMs = 1500): Promise { - return new Promise((resolve) => { +function waitForFirstUpdate(state: SharedState, timeoutMs = 1500): Promise { + return new Promise((resolve) => { let settled = false - const finish = () => { + const finish = (updated: boolean) => { if (settled) return settled = true off() - resolve() + resolve(updated) } - const off = state.on('updated', finish) - setTimeout(finish, timeoutMs) + const off = state.on('updated', () => finish(true)) + setTimeout(() => finish(false), timeoutMs) }) } @@ -104,12 +104,17 @@ async function ensureShared(): Promise> { const state = await client.sharedState.get('ghfs:cards-pile', { initialValue: { pile: null }, }) - // The client returns the local initialValue immediately; the server - // reply arrives asynchronously and fires `updated`. Block hydrate - // on it so the page doesn't flash empty → loaded for a saved pile. - await waitForFirstUpdate(state) - applyServerState(state.value().pile ?? null) + // Subscribe before the first read. `sharedState.get` applies the server + // reply through a second `mutate`, which fires `updated` again, and that + // reply can land after `start()` has already written a fresh pile. + // Missing it lets the empty initial value overwrite the pile the user + // just created. state.on('updated', (next) => applyServerState(next.pile ?? null)) + // Only read back when the server actually replied. On timeout the value + // is still the empty initialValue, and applying it would wipe a pile + // `start()` wrote while this promise was in flight. + if (await waitForFirstUpdate(state)) + applyServerState(state.value().pile ?? null) sharedHandle = state return state })() @@ -291,16 +296,25 @@ function pickFromCandidates( } function snapshot(): CardsPileState { + // `source` and `options` live in reactive() proxies. immer freezes the + // draft and then reads these back; a Vue proxy refuses that read, so hand + // immer plain objects. return { - pile: pile.value, + pile: pile.value.map(card => ({ ...card })), index: index.value, - processedOps: processedOps.value, - source: source.value, - options: options.value, + processedOps: processedOps.value.map(op => ({ ...op })), + source: { ...toRaw(source.value) }, + options: { ...toRaw(options.value) }, } } function applyServerState(next: CardsPileState | null): void { + // A server echo can arrive after `start()` has already built a pile locally + // but before that pile is persisted. Applying the stale empty value here + // wipes it, and the following persist then saves the empty pile. + const incoming = next?.pile.length ?? 0 + if (incoming === 0 && pile.value.length > 0) + return if (!next) { pile.value = [] index.value = 0 From 89f0fcb20ac4e201c43f5f47468f7f89a0d93231 Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 17 Nov 2026 12:48:52 +0800 Subject: [PATCH 2/2] test(ui): cover a late empty echo wiping a new card pile sharedState.get resolves after start() has already built the pile. The test emits the empty server value in that window, which used to clear the pile and persist it. --- ui/composables/useCardsMode.test.ts | 112 ++++++++++++++++++++++++++++ vitest.config.ts | 10 +++ 2 files changed, 122 insertions(+) create mode 100644 ui/composables/useCardsMode.test.ts diff --git a/ui/composables/useCardsMode.test.ts b/ui/composables/useCardsMode.test.ts new file mode 100644 index 00000000..4f45d2b5 --- /dev/null +++ b/ui/composables/useCardsMode.test.ts @@ -0,0 +1,112 @@ +import { describe, expect, it, vi } from 'vitest' +import type { CardsPileState, CardsSharedState } from '../../src/devframe/rpc/types' +import type { ListItem } from '../types/list-item' +import { DEFAULT_PILE_OPTIONS, useCardsMode } from './useCardsMode' + +type Listener = (next: CardsSharedState) => void + +const shared = vi.hoisted(() => { + const listeners = new Set() + const handle = { + current: { pile: null } as CardsSharedState, + value(): CardsSharedState { + return this.current + }, + on(_event: 'updated', fn: Listener): () => void { + listeners.add(fn) + return () => { + listeners.delete(fn) + } + }, + mutate(recipe: (draft: CardsSharedState) => void): void { + recipe(this.current) + for (const fn of listeners) + fn(this.current) + }, + emit(next: CardsSharedState): void { + this.current = next + for (const fn of listeners) + fn(next) + }, + } + let resolveGet: (state: typeof handle) => void = () => {} + const pendingGet = new Promise((resolve) => { + resolveGet = resolve + }) + return { handle, pendingGet, resolveGet } +}) + +vi.mock('#imports', () => ({ + useRouter: () => ({ + currentRoute: { value: { path: '/' } }, + push: async () => {}, + }), + useRoute: () => ({ path: '/' }), +})) + +vi.mock('./useRpc', () => ({ + useRpcClient: async () => ({ + sharedState: { + get: () => shared.pendingGet, + }, + }), +})) + +vi.mock('./useSeenHistory', () => ({ + useSeenHistory: () => ({ getSeenEntry: () => undefined }), +})) + +vi.mock('./useUiState', () => ({ + useUiState: () => ({ + isTodo: () => false, + isIgnored: () => false, + }), +})) + +vi.mock('./useProjectPayload', () => ({ + useProjectPayload: () => ({ ensureLoaded: async () => {} }), +})) + +function item(number: number): ListItem { + return { + key: `p-issue-${number}`, + projectId: 'p', + repo: 'o/r', + kind: 'issue', + number, + title: `Issue ${number}`, + state: 'open', + author: 'octocat', + updatedAt: `2026-01-0${number}T00:00:00.000Z`, + } +} + +const emptyPile: CardsPileState = { + pile: [], + index: 0, + processedOps: [], + source: { label: 'Cards' }, + options: { ...DEFAULT_PILE_OPTIONS }, +} + +describe('useCardsMode start', () => { + it('keeps a pile built locally when the server then echoes an empty one', async () => { + const cards = useCardsMode() + const started = cards.start( + [item(1), item(2)], + { label: 'Cards' }, + { ...DEFAULT_PILE_OPTIONS, excludeSeen: false, size: 10 }, + null, + ) + + await Promise.resolve() + shared.resolveGet(shared.handle) + await Promise.resolve() + shared.handle.emit({ pile: emptyPile }) + + await started + + expect(cards.pile.value.map(card => card.number)).toEqual([2, 1]) + expect(shared.handle.current.pile?.pile.map(card => card.number)).toEqual([2, 1]) + }) +}) diff --git a/vitest.config.ts b/vitest.config.ts index 7b210d5e..32c560ba 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,16 @@ +import { fileURLToPath } from 'node:url' import { defineConfig } from 'vitest/config' +const fromRoot = (path: string) => fileURLToPath(new URL(path, import.meta.url)) + export default defineConfig({ + resolve: { + alias: { + // The root package does not depend on vue, but the UI does. Point + // tests at the copy pnpm already installed for it. + vue: fromRoot('./node_modules/.pnpm/node_modules/vue'), + }, + }, test: { exclude: [ '**/node_modules/**',