Wire the offline write-queue into the desktop write path

The offline write-queue (packages/core/src/queue/write-queue-v0.ts, #33) was a
tested pure module that nothing imported. Approving a change while gitea was
unreachable made gitea:applyChange call the client directly and throw, losing
the write. Now it's wired end to end:

- queue-store.ts persists the queue next to the snapshot store (same
  degrade-to-empty-on-corruption discipline; injectable path for tests).
- applyChange extracts the guarded write into applyChangeLive and, on
  unreachability (any error that is NOT a GiteaApiError rejection), enqueues the
  intent — coalesced by (issue, axis) — instead of throwing, returning
  { ok, queued, pending }. A genuine GiteaApiError still surfaces (a doomed
  write must not replay forever).
- reconcile drains the queue once a successful read proves gitea is reachable,
  re-reading so the board reflects the replays; replays are idempotent
  (label plan.noop, assignee/milestone re-set). boot + stale reconcile report
  the pending count so the badge shows immediately offline.
- Renderer: use-backlog threads `pending`; the OfflineBanner shows "N queued";
  the chat approve message distinguishes a queued (offline) approval from an
  applied one.

Also wires vitest into the desktop workspace (was missing, so the main-process
suite couldn't run via `yarn test`) and fixes a stale Capture copy assertion
left by the earlier posh-copy pass.

Tests: queue-store.test.ts (persist/reload/coalesce/replay-drain, real core fns);
9 main-process + 169 core green; 12 demo e2e green. A one-off GITEA_LIVE smoke
verified an online write lands+reverts and an offline approve queues+drains
against the real repo (not committed, per the repo's no-mutating-test convention).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Croissant Le Doux
2026-07-12 19:55:45 -04:00
parent c46306e3ee
commit 9370c04ac5
11 changed files with 239 additions and 37 deletions

View File

@@ -0,0 +1,87 @@
/**
* Durable offline write-queue store (#33). The invariant mirrors the snapshot
* store's purity: the queue file is a convenience mirror, and a missing or corrupt
* file must degrade to "empty queue", never a crash — losing a queued write is
* bad, but crashing the whole write path is worse. These tests also exercise the
* store together with core's coalescing/replay so the round-trip an offline edit
* takes (enqueue → persist → reload → replay → drain) is covered end to end.
*/
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { enqueueWrite, replayQueue, type IssueChange } from '@commitea/core'
import { afterEach, describe, expect, it, vi } from 'vitest'
// electron can't be imported outside the Electron runtime; the store only needs
// app.getPath for its default path, which every test overrides with an injected path.
vi.mock('electron', () => ({ app: { getPath: () => tmpdir() } }))
import { loadQueue, saveQueue } from './queue-store.js'
const estChange = (issue: number, estimate = 'est/5d'): IssueChange =>
({ kind: 'reestimate', issue, estimate }) as IssueChange
const assignChange = (issue: number, assignee: string | null): IssueChange =>
({ kind: 'assign', issue, assignee }) as IssueChange
describe('queue-store (#33)', () => {
let dir: string | null = null
const path = () => {
if (!dir) dir = mkdtempSync(join(tmpdir(), 'commitea-queue-'))
return join(dir, 'commitea-write-queue.json')
}
afterEach(() => {
if (dir) rmSync(dir, { recursive: true, force: true })
dir = null
})
it('round-trips a persisted queue', () => {
const p = path()
const queue = enqueueWrite([], estChange(1), '2026-07-11T00:00:00Z')
saveQueue(queue, p)
expect(loadQueue(p)).toEqual(queue)
})
it('a missing file degrades to an empty queue (never a crash)', () => {
// nothing written yet — load must return [] so the write path stays alive
expect(loadQueue(path())).toEqual([])
})
it('a corrupt or wrong-shape file is treated as an empty queue', () => {
const p = path()
writeFileSync(p, '{ not json', 'utf8')
expect(loadQueue(p)).toEqual([])
// valid JSON but not an array (drift) is also rejected
writeFileSync(p, JSON.stringify({ nope: true }), 'utf8')
expect(loadQueue(p)).toEqual([])
})
it('persisted queue coalesces by (issue, axis) across saves — the later intent wins', () => {
const p = path()
// two estimates for the same issue, queued while offline: only the last survives
saveQueue(enqueueWrite(loadQueue(p), estChange(1, 'est/2d'), '2026-07-11T00:00:00Z'), p)
saveQueue(enqueueWrite(loadQueue(p), estChange(1, 'est/8d'), '2026-07-11T00:01:00Z'), p)
// a different axis on the same issue coexists
saveQueue(enqueueWrite(loadQueue(p), assignChange(1, 'ana'), '2026-07-11T00:02:00Z'), p)
const queue = loadQueue(p)
expect(queue).toHaveLength(2)
expect(queue.map((w) => w.change)).toEqual([estChange(1, 'est/8d'), assignChange(1, 'ana')])
})
it('reloads and replays the queue, draining what applies and keeping what fails', async () => {
const p = path()
saveQueue(enqueueWrite(loadQueue(p), estChange(1), '2026-07-11T00:00:00Z'), p)
saveQueue(enqueueWrite(loadQueue(p), assignChange(2, 'ana'), '2026-07-11T00:01:00Z'), p)
// Simulate reconnect: replay through an apply that fails only for issue #2
// (still unreachable / rejected). The successful write drains; the other stays.
const { drained, remaining } = await replayQueue(loadQueue(p), async (c) =>
c.issue === 2 ? { ok: false } : { ok: true },
)
saveQueue(remaining, p)
expect(drained.map((w) => w.change.issue)).toEqual([1])
expect(loadQueue(p).map((w) => w.change.issue)).toEqual([2])
})
})