Skip to content

AI, fully local on your computer.

30.7 KB
Raw Blame
// @vitest-environment jsdom
import { describe, it, expect, vi, afterEach } from 'vitest'
import {
  acknowledgeError,
  adoptActiveJob,
  clearTranslateSession,
  getTranslateSession,
  stopActive,
  translate
} from '../../src/renderer/lib/translateSession'
import { clearFileTranslate, getFileTranslate, translateDroppedFiles } from '../../src/renderer/lib/fileTranslateSession'
import { clearDocTaskSession } from '../../src/renderer/lib/doctasks'
import type { TranslateJob } from '../../src/shared/types'
import type { PreloadApi } from '../../src/preload'
import { stubApi, assertNoUnexpectedApiCalls } from '../helpers/renderer'

// F-41 (audit-2026-07-16): stub payloads are typed against the real PreloadApi bridge contract
// (no `as never` erasure) — a rename of any mocked method or of TranslateJob reddens typecheck.
// The one intentional exception (a malformed `translateStart` resolve, L6b) is expressed as a
// narrow `as unknown as TranslateJob` at exactly the violating field, not a blanket erasure.

// Unit test for the Translate-view module store (TG-4, plan §2 D6): the remount recovery
// (`adoptActiveJob`), the one-at-a-time guard, and the generation guards. The lock-time content
// purge is pinned in lockPurge.test.ts. The TranslateScreen renderer suite covers the happy
// translate→stream→copy path through this store; this file targets the recovery/guard edges directly.

afterEach(() => {
  clearTranslateSession()
  // translate()'s cross-session start guard (L6a) reads the file + doc-task stores — reset them too.
  clearFileTranslate()
  clearDocTaskSession()
  assertNoUnexpectedApiCalls()
})

describe('translateSession — remount recovery + guards', () => {
  it('adoptActiveJob re-adopts a still-running job from main and seeds its partial text', async () => {
    const token: { fn?: (t: string) => void } = {}
    const running: TranslateJob = { jobId: 'j9', state: 'translating', text: 'Halb ' }
    stubApi({
      getActiveTranslateJob: vi.fn(async () => running),
      onTranslateToken: vi.fn((_id: string, cb: (t: string) => void) => {
        token.fn = cb
        return () => {}
      }),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    await adoptActiveJob()
    let snap = getTranslateSession()
    expect(snap.activeJobId).toBe('j9')
    expect(snap.output).toBe('Halb ')
    expect(snap.translating).toBe(true)

    // Newly streamed tokens append onto the seeded text.
    token.fn?.('fertig')
    snap = getTranslateSession()
    expect(snap.output).toBe('Halb fertig')
  })

  it('adoptActiveJob is a no-op when main reports no running job', async () => {
    stubApi({ getActiveTranslateJob: vi.fn(async () => null) })
    await adoptActiveJob()
    expect(getTranslateSession().activeJobId).toBeNull()
    expect(getTranslateSession().state).toBe('idle')
  })

  it('translate() busy-guards a second run while one is active', async () => {
    stubApi({
      getActiveTranslateJob: vi.fn(async () => null),
      translateStart: vi.fn(async () => ({ jobId: 'j1', state: 'queued', text: '' }) as TranslateJob),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    const first = await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(first).toBe('started')
    const second = await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(second).toBe('busy')
  })

  it('translate() no-ops on empty text', async () => {
    stubApi({})
    expect(await translate({ sourceLang: 'de', targetLang: 'en', text: '   ' })).toBe('noop')
  })

  it('cancels the orphan job and does not wire a stream when superseded during the start round-trip', async () => {
    // The generation guard (invariant 6): a clear/lock/new-translate that lands WHILE translateStart
    // is in flight must bump startGen so the slow resolve bails — cancelling its now-orphan job —
    // instead of wiring a zombie stream a stale done/error would tear down over a newer job.
    let resolveStart!: (j: TranslateJob) => void
    const startPromise = new Promise<TranslateJob>((r) => (resolveStart = r))
    const cancel = vi.fn(async () => ({ jobId: 'orph', state: 'cancelled' }) as TranslateJob)
    stubApi({
      getActiveTranslateJob: vi.fn(async () => null),
      translateStart: vi.fn(() => startPromise),
      translateCancel: cancel,
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    const p = translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    clearTranslateSession() // supersede while the start round-trip is in flight
    resolveStart({ jobId: 'orph', state: 'queued', text: '' })
    expect(await p).toBe('started')

    expect(cancel).toHaveBeenCalledWith('orph') // the orphan job was cancelled main-side
    expect(getTranslateSession().activeJobId).toBeNull() // no zombie stream wired
    expect(getTranslateSession().state).toBe('idle') // stayed cleared, not 'translating'
  })

  it('Stop during the start round-trip supersedes and cancels the just-started job (L5)', async () => {
    // Stop shows as soon as `translating` is true — before translateStart resolves and sets
    // activeJobId. stopActive() must bump the generation so the in-flight start's post-await branch
    // sees itself superseded and cancels the orphan; otherwise Stop is silently swallowed.
    let resolveStart!: (j: TranslateJob) => void
    const startPromise = new Promise<TranslateJob>((r) => (resolveStart = r))
    const cancel = vi.fn(async () => ({ jobId: 'j1', state: 'cancelled' }) as TranslateJob)
    stubApi({
      getActiveTranslateJob: vi.fn(async () => null),
      translateStart: vi.fn(() => startPromise),
      translateCancel: cancel,
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    const p = translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    // Mid start round-trip: translating true, no job id yet.
    expect(getTranslateSession().translating).toBe(true)
    expect(getTranslateSession().activeJobId).toBeNull()

    stopActive() // Stop with no activeJobId yet — must still supersede.
    expect(getTranslateSession().state).toBe('cancelled')
    expect(getTranslateSession().translating).toBe(false)

    resolveStart({ jobId: 'j1', state: 'queued', text: '' })
    expect(await p).toBe('started')
    expect(cancel).toHaveBeenCalledWith('j1') // the just-started job was cancelled main-side
    expect(getTranslateSession().activeJobId).toBeNull() // no zombie stream wired
  })

  it('busy-guards a second translate during the start round-trip (L6a translating gap)', async () => {
    // The old entry guard checked only activeJobId, so a second click WHILE the first start round-trip
    // was in flight (translating, no id yet) slipped through and started a second job.
    let resolveStart!: (j: TranslateJob) => void
    const startPromise = new Promise<TranslateJob>((r) => (resolveStart = r))
    const translateStart = vi.fn(() => startPromise)
    stubApi({
      getActiveTranslateJob: vi.fn(async () => null),
      translateStart,
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    const first = translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    const second = await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(second).toBe('busy')
    expect(translateStart).toHaveBeenCalledTimes(1)

    resolveStart({ jobId: 'j1', state: 'queued', text: '' })
    await first
  })

  it('refuses while a DOCUMENT translation is in flight — no second job on the lane (L6a, #682)', async () => {
    // The screen's one `busy` disables Translate while a file translation runs, so this guard is the
    // only thing between a stray call and a second job on the one-at-a-time lane.
    const translateStart = vi.fn(async () => ({ jobId: 'j1', state: 'queued', text: '' }) as TranslateJob)
    stubApi({
      getDroppedFilePath: vi.fn(() => 'C:\\docs\\a.pdf'),
      importDocuments: vi.fn(() => new Promise<never>(() => {})), // the import stays in flight
      translateStart
    })
    void translateDroppedFiles([new File(['%PDF'], 'a.pdf')], { sourceLang: 'de', targetLang: 'en' })
    expect(getFileTranslate().busy).toBe(true) // busy from the start, before the import's first await

    expect(await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })).toBe('busy')
    expect(translateStart).not.toHaveBeenCalled()
    expect(getTranslateSession().state).toBe('idle') // the refusal took nothing from the document's panel
  })

  it('a malformed translateStart resolve does not wedge the store stuck translating (L6b)', async () => {
    // job.error was dereferenced outside any try; an undefined/malformed bridge resolve threw OUT of
    // translate() (whose .then caller has no .catch), leaving the store stuck translating. The
    // defensive resolve handling now treats it as a runtime failure and resets translating.
    stubApi({
      getActiveTranslateJob: vi.fn(async () => null),
      // Deliberately contract-violating: the bridge type promises a TranslateJob but this
      // malformed resolve yields undefined (the L6b defensive-handling case under test).
      translateStart: vi.fn(async () => undefined as unknown as TranslateJob),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })

    const outcome = await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(outcome).toBe('started')
    const snap = getTranslateSession()
    expect(snap.state).toBe('failed')
    expect(snap.error).toBe('runtimeFailed')
    expect(snap.translating).toBe(false) // not wedged: Stop is no longer dead
  })

})

// ---- The mount adopt runs on EVERY Translate mount, not only after a renderer reload ----
//
// `adoptActiveJob` used to gate on `activeJobId` alone, and that id is NULL in every TERMINAL state
// (the done/error stream handlers and `stopActive` all clear it) — so the guard read "nothing here"
// on a panel that was showing a finished / failed / just-cancelled translation, and the adopt then
// replaced the held text with `job.text ?? ''` and flipped the panel back to "translating". These
// tests pin the corrected rule: adopt only into a GENUINELY EMPTY store.

describe('translateSession — adoptActiveJob must not clobber a held session', () => {
  /** Stream stubs that hand the per-job token/done/error callbacks back to the test. */
  function streamStubs(): {
    api: Partial<PreloadApi>
    token: { fn?: (t: string) => void }
    done: { fn?: (j: TranslateJob) => void }
    error: { fn?: (j: TranslateJob) => void }
    cancel: ReturnType<typeof vi.fn>
  } {
    const token: { fn?: (t: string) => void } = {}
    const done: { fn?: (j: TranslateJob) => void } = {}
    const error: { fn?: (j: TranslateJob) => void } = {}
    const cancel = vi.fn(async () => ({ jobId: 'j1', state: 'cancelled' }) as TranslateJob)
    return {
      token,
      done,
      error,
      cancel,
      api: {
        getActiveTranslateJob: vi.fn(async (): Promise<TranslateJob | null> => null),
        translateStart: vi.fn(async () => ({ jobId: 'j1', state: 'translating', text: '' }) as TranslateJob),
        translateCancel: cancel,
        onTranslateToken: vi.fn((_id: string, cb: (t: string) => void) => {
          token.fn = cb
          return () => {}
        }),
        onTranslateDone: vi.fn((_id: string, cb: (j: TranslateJob) => void) => {
          done.fn = cb
          return () => {}
        }),
        onTranslateError: vi.fn((_id: string, cb: (j: TranslateJob) => void) => {
          error.fn = cb
          return () => {}
        })
      }
    }
  }

  it('does NOT wipe a CANCELLED translation the user stopped, when the panel is re-entered', async () => {
    // The reachable path. Stop cancels main-side via `translateCancel(...)`, whose rejection is
    // deliberately swallowed so a failed cancel can never break the UI — so main can keep the job in
    // `translating` while this store shows `cancelled` with the partial output the user chose to
    // keep. `activeJobId` is null there, so the old id-only guard let the next mount adopt that
    // still-"running" job straight over the held result.
    const s = streamStubs()
    stubApi(s.api)
    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo Welt' })
    s.token.fn?.('Hello ')
    s.token.fn?.('world')
    stopActive()

    expect(s.cancel).toHaveBeenCalledWith('j1') // the cancel WAS issued — its outcome is unobservable

    const held = getTranslateSession()
    expect(held.state).toBe('cancelled')
    expect(held.output).toBe('Hello world')
    expect(held.activeJobId).toBeNull() // ← what the old guard mistook for "this store is empty"
    expect(held.translating).toBe(false)

    // Back on the Translate screen: the mount effect re-runs the adopt while main — never having
    // processed the cancel — still reports the job as running.
    const adoptApi = {
      getActiveTranslateJob: vi.fn(async (): Promise<TranslateJob> => ({ jobId: 'j1', state: 'translating', text: 'Hello wo' })),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(adoptApi)
    await adoptActiveJob()

    const snap = getTranslateSession()
    expect(snap.state).toBe('cancelled') // not flipped back to 'translating'
    expect(snap.output).toBe('Hello world') // not replaced by the job's shorter accumulated text
    expect(snap.activeJobId).toBeNull()
    expect(snap.translating).toBe(false)
    expect(snap).toBe(held) // nothing was written at all (same snapshot object)
    expect(adoptApi.onTranslateToken).not.toHaveBeenCalled() // no stream wired over the held result
  })

  it('does NOT wipe a DONE translation the user is still reading', async () => {
    // Same invariant on the other terminal state: `done` also clears `activeJobId`, and the panel
    // then renders the finished text with a Copy action. Whatever main reports afterwards, a mount
    // must not replace a completed result with a fresh "translating" frame.
    const s = streamStubs()
    stubApi(s.api)
    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Guten Tag' })
    s.done.fn?.({ jobId: 'j1', state: 'done', text: 'Good day.' })

    const finished = getTranslateSession()
    expect(finished.state).toBe('done')
    expect(finished.output).toBe('Good day.')
    expect(finished.activeJobId).toBeNull()

    const adoptApi = {
      getActiveTranslateJob: vi.fn(async (): Promise<TranslateJob> => ({ jobId: 'j1', state: 'translating', text: 'Good' })),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(adoptApi)
    await adoptActiveJob()

    const snap = getTranslateSession()
    expect(snap.state).toBe('done')
    expect(snap.output).toBe('Good day.')
    expect(snap.translating).toBe(false)
    expect(snap).toBe(finished)
    expect(adoptApi.onTranslateToken).not.toHaveBeenCalled()
  })

  it('does NOT wipe the partial output kept after a failed banner was dismissed', async () => {
    // `acknowledgeError` parks the store at `idle` while deliberately KEEPING the partial text (the
    // panel still renders it, with Copy) — so `idle` alone does not mean empty in this store, and
    // the emptiness gate checks the output too.
    const s = streamStubs()
    stubApi(s.api)
    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo Welt' })
    s.token.fn?.('Half a ')
    s.token.fn?.('translation')
    s.error.fn?.({ jobId: 'j1', state: 'failed', error: 'runtimeFailed' })
    expect(getTranslateSession().state).toBe('failed')
    acknowledgeError() // the user dismisses the banner; the partial text stays on screen

    const kept = getTranslateSession()
    expect(kept.state).toBe('idle')
    expect(kept.output).toBe('Half a translation')

    const adoptApi = {
      getActiveTranslateJob: vi.fn(async (): Promise<TranslateJob> => ({ jobId: 'j2', state: 'translating', text: '' })),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(adoptApi)
    await adoptActiveJob()

    const snap = getTranslateSession()
    expect(snap.output).toBe('Half a translation')
    expect(snap.state).toBe('idle')
    expect(snap).toBe(kept)
    expect(adoptApi.onTranslateToken).not.toHaveBeenCalled()
  })

  it('bails when a translate STARTED while the active-job read was in flight (post-await re-check)', async () => {
    // The re-check must enforce the SAME emptiness rule as the entry guard, or the no-op is true
    // only at function entry and not at the moment of the destructive `set`. `translate()` flips the
    // store to `translating` SYNCHRONOUSLY, before its own round-trip resolves and sets
    // `activeJobId` — so an id-only re-check sailed straight past a translation the user had just
    // started: it seeded the older job's text AND bumped `startGen`, which made the in-flight
    // start cancel the user's brand-new job as an orphan.
    let resolveActive!: (j: TranslateJob) => void
    const activeP = new Promise<TranslateJob>((r) => (resolveActive = r))
    let resolveStart!: (j: TranslateJob) => void
    const startP = new Promise<TranslateJob>((r) => (resolveStart = r))
    const wired: string[] = []
    const cancel = vi.fn(async () => ({ jobId: 'fresh', state: 'cancelled' }) as TranslateJob)
    const api = {
      getActiveTranslateJob: vi.fn(() => activeP),
      translateStart: vi.fn(() => startP),
      translateCancel: cancel,
      onTranslateToken: vi.fn((id: string) => {
        wired.push(id)
        return () => {}
      }),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(api)

    // The mount adopt is parked on the active-job read.
    const adoptPromise = adoptActiveJob()
    expect(api.getActiveTranslateJob).toHaveBeenCalledTimes(1) // called before the adopt's first await

    // The user hits Translate in that window: translating, no job id yet.
    const translatePromise = translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(getTranslateSession().state).toBe('translating')
    expect(getTranslateSession().activeJobId).toBeNull()

    resolveActive({ jobId: 'stale', state: 'translating', text: 'alte Übersetzung' })
    await adoptPromise

    // The adopt bailed: it neither seeded the older job nor superseded the in-flight start.
    expect(getTranslateSession().output).toBe('')
    expect(getTranslateSession().activeJobId).toBeNull()

    resolveStart({ jobId: 'fresh', state: 'translating', text: '' })
    expect(await translatePromise).toBe('started')
    expect(getTranslateSession().activeJobId).toBe('fresh') // the user's own job owns the panel
    expect(cancel).not.toHaveBeenCalled() // it was not cancelled as a supposed orphan
    expect(wired).toEqual(['fresh']) // exactly one stream, on the new job
  })
})

// ---- The adopt's await window must be guarded by the GENERATION, not by emptiness alone ----
//
// Emptiness answers "is anything on screen right now?", which is the wrong question for work that
// happened DURING the active-job read. A workspace lock is the case that breaks the emptiness rule
// outright: the lock purge resets this store to EMPTY, so a store that was legitimately invalidated
// mid-read looks identical to the post-reload store the adopt exists for. Only a generation token
// captured BEFORE the await — and re-checked after it — can tell the two apart.

describe('translateSession — adoptActiveJob must not re-seed content a lock purge dropped', () => {
  it('bails when a workspace LOCK purged the store while the active-job read was in flight', async () => {
    // `App.lockNow` purges every session store AFTER main aborted the job, purged its job map and
    // re-encrypted the vault. If the `getActiveTranslateJob` read resolved with a still-running job
    // just before that purge landed, an emptiness-only guard reads the freshly-EMPTIED store as
    // "safe to adopt" and writes `job.text` straight back in — plus a live stream subscription —
    // leaving plaintext translation content resident in renderer memory for the whole locked period
    // (it is not rendered, the lock unmounts the screen, but it is resident until the next purge).
    let resolveActive!: (j: TranslateJob) => void
    const activeP = new Promise<TranslateJob>((r) => (resolveActive = r))
    const api = {
      getActiveTranslateJob: vi.fn(() => activeP),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(api)

    // The mount adopt is parked on the active-job read.
    const adoptPromise = adoptActiveJob()
    expect(api.getActiveTranslateJob).toHaveBeenCalledTimes(1) // called before the adopt's first await

    clearTranslateSession() // the workspace locks in that window (the renderer lock seam)
    const purged = getTranslateSession()

    resolveActive({ jobId: 'j-locked', state: 'translating', text: 'vertraulicher Text' })
    await adoptPromise

    const snap = getTranslateSession()
    expect(snap.output).toBe('') // no plaintext re-seeded behind the lock
    expect(snap.state).toBe('idle')
    expect(snap.activeJobId).toBeNull()
    expect(snap.translating).toBe(false)
    expect(snap).toBe(purged) // nothing was written at all (same snapshot object)
    expect(api.onTranslateToken).not.toHaveBeenCalled() // no stream wired over the locked workspace
  })

  it('bails when the user STOPPED a translation while the active-job read was in flight', async () => {
    // The other invalidation that must reach a parked adopt. Stop leaves the store at `cancelled`
    // (non-empty) AND bumps the generation, so the adopt refuses under either rule — pinned so a
    // change to one of them cannot quietly re-open the window.
    let resolveActive!: (j: TranslateJob) => void
    const activeP = new Promise<TranslateJob>((r) => (resolveActive = r))
    const api = {
      getActiveTranslateJob: vi.fn(() => activeP),
      translateStart: vi.fn(async () => ({ jobId: 'j1', state: 'translating', text: '' }) as TranslateJob),
      translateCancel: vi.fn(async () => ({ jobId: 'j1', state: 'cancelled' }) as TranslateJob),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(api)

    const adoptPromise = adoptActiveJob()
    expect(api.getActiveTranslateJob).toHaveBeenCalledTimes(1) // called before the adopt's first await

    // The user runs their own translation and stops it while the adopt is still parked.
    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    stopActive()
    const stopped = getTranslateSession()
    expect(stopped.state).toBe('cancelled')

    resolveActive({ jobId: 'stale', state: 'translating', text: 'alte Übersetzung' })
    await adoptPromise

    expect(getTranslateSession()).toBe(stopped) // the stopped session was left exactly as it was
    expect(getTranslateSession().output).toBe('')
  })

  it('CONTROL: still adopts when nothing invalidates the store during a slow active-job read', async () => {
    // The guard must not cost the case the adopt exists for. A renderer reload leaves an EMPTY store
    // and a job still running in main; a slow read with no intervening action must still seed the
    // accumulated text and resubscribe. The earlier lock is deliberate: the generation is a running
    // counter, so the token has to be READ at entry, never assumed to start at zero.
    clearTranslateSession() // a previous lock already moved the generation on
    let resolveActive!: (j: TranslateJob) => void
    const activeP = new Promise<TranslateJob>((r) => (resolveActive = r))
    const token: { fn?: (t: string) => void } = {}
    const api = {
      getActiveTranslateJob: vi.fn(() => activeP),
      onTranslateToken: vi.fn((_id: string, cb: (t: string) => void) => {
        token.fn = cb
        return () => {}
      }),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    }
    stubApi(api)

    const adoptPromise = adoptActiveJob()
    expect(api.getActiveTranslateJob).toHaveBeenCalledTimes(1) // called before the adopt's first await
    // QUEUED, not translating: the text adopt accepts both (translateSession.ts:317); the base adopt above covers translating.
    resolveActive({ jobId: 'j5', state: 'queued', text: 'Teil ' })
    await adoptPromise

    const snap = getTranslateSession()
    expect(snap.activeJobId).toBe('j5')
    expect(snap.output).toBe('Teil ')
    expect(snap.state).toBe('translating')
    expect(snap.translating).toBe(true) // Stop is live again
    token.fn?.('zwei')
    expect(getTranslateSession().output).toBe('Teil zwei') // the live stream really is wired
  })
})

// ---- #163 (T-6): translateSession edges — a REJECTING translateStart, and the stale-token guard ----

describe('translateSession — #163 (T-6) edges', () => {
  it('a REJECTING translateStart lands the store in failed (never wedged in translating)', async () => {
    // Only the malformed-RESOLVE sibling (L6b) was tested; an IPC THROW — a destroyed handler,
    // a serialization fault — must reach `failed` with a dead-safe Stop, not wedge the store
    // in `translating` forever (TranslateScreen's .then chain has no .catch).
    stubApi({
      translateStart: vi.fn(async () => {
        throw new Error('ipc broke')
      }),
      onTranslateToken: vi.fn(() => () => {}),
      onTranslateDone: vi.fn(() => () => {}),
      onTranslateError: vi.fn(() => () => {})
    })
    const outcome = await translate({ sourceLang: 'de', targetLang: 'en', text: 'Hallo' })
    expect(outcome).toBe('started')
    const snap = getTranslateSession()
    expect(snap.state).toBe('failed')
    expect(snap.error).toBe('runtimeFailed')
    expect(snap.translating).toBe(false)
    expect(snap.activeJobId).toBeNull()
  })

  it('the stale-token guard: a late event for a SUPERSEDED job never mutates the newer session', async () => {
    // Capture job A's stream callbacks, then supersede A with a fresh translate (job B). A's
    // late token/done events — a slow IPC hop delivering after the switch — must be ignored
    // (the wireStream guard keys on snapshot.activeJobId).
    const callbacks = new Map<string, { token?: (t: string) => void; done?: (j: TranslateJob) => void }>()
    let nextJob = 'jA'
    stubApi({
      translateStart: vi.fn(async () => ({ jobId: nextJob, state: 'queued', text: '' }) as TranslateJob),
      onTranslateToken: vi.fn((id: string, cb: (t: string) => void) => {
        callbacks.set(id, { ...callbacks.get(id), token: cb })
        return () => {}
      }),
      onTranslateDone: vi.fn((id: string, cb: (j: TranslateJob) => void) => {
        callbacks.set(id, { ...callbacks.get(id), done: cb })
        return () => {}
      }),
      onTranslateError: vi.fn(() => () => {}),
      translateCancel: vi.fn(async () => ({ jobId: 'jA', state: 'cancelled' }) as TranslateJob)
    })

    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Erster' })
    expect(getTranslateSession().activeJobId).toBe('jA')

    // Stop A, then start B — the session now belongs to jB.
    stopActive()
    nextJob = 'jB'
    await translate({ sourceLang: 'de', targetLang: 'en', text: 'Zweiter' })
    expect(getTranslateSession().activeJobId).toBe('jB')
    const before = getTranslateSession()

    // A's LATE events arrive. The guard must drop them: output untouched, state untouched.
    callbacks.get('jA')?.token?.('GEISTERTEXT ')
    callbacks.get('jA')?.done?.({ jobId: 'jA', state: 'done', text: 'GEISTERTEXT' })
    const after = getTranslateSession()
    expect(after.output).not.toContain('GEISTERTEXT')
    expect(after.activeJobId).toBe('jB')
    expect(after.translating).toBe(true)
    expect(after.state).toBe(before.state)

    // B's own stream still works.
    callbacks.get('jB')?.token?.('echt')
    expect(getTranslateSession().output).toBe('echt')
  })
})

// ---- DOC-7: the pair a translate surface opens on (#682) ----
//
// `initialTranslateChoice` is the one rule the Translate screen and the Documents translate modal both
// open on: the session's last pair, else the UI-language default. The memory is module-level and
// nothing resets it (the lock purge keeps it by design), so each row runs on a FRESH module graph:
// its memory starts empty, and nothing it writes reaches another row.

describe('translateSession — the pair a translate surface opens on (DOC-7, #682)', () => {
  type Fresh = {
    session: typeof import('../../src/renderer/lib/translateSession')
    file: typeof import('../../src/renderer/lib/fileTranslateSession')
  }
  async function freshStores(): Promise<Fresh> {
    vi.resetModules()
    const session = await import('../../src/renderer/lib/translateSession')
    const file = await import('../../src/renderer/lib/fileTranslateSession')
    return { session, file }
  }

  it.each([
    { lang: 'en', pair: { sourceLang: 'de', targetLang: 'en' } },
    { lang: 'de', pair: { sourceLang: 'en', targetLang: 'de' } },
    // Any UI language but English opens on English → itself (the rule reads no registry).
    { lang: 'ru', pair: { sourceLang: 'en', targetLang: 'ru' } }
  ] as const)('before any translation this session, a $lang UI opens on its default pair', async ({ lang, pair }) => {
    const { session } = await freshStores()
    expect(session.initialTranslateChoice(lang)).toEqual(pair)
  })

  // The pair is remembered as the translation STARTS, before its first round-trip — both round-trips
  // stay in flight here, so nothing runs past the write.
  it.each([
    {
      label: 'a text translation',
      start: ({ session }: Fresh) => session.translate({ sourceLang: 'fr', targetLang: 'pl', text: 'Bonjour' })
    },
    {
      label: 'a document translation',
      start: ({ file }: Fresh) =>
        file.translateDroppedFiles([new File(['%PDF'], 'a.pdf')], { sourceLang: 'fr', targetLang: 'pl' })
    }
  ])('once $label starts, every surface opens on its pair whatever the UI language', async ({ start }) => {
    stubApi({
      translateStart: vi.fn(() => new Promise<never>(() => {})),
      getDroppedFilePath: vi.fn(() => 'C:\\docs\\a.pdf'),
      importDocuments: vi.fn(() => new Promise<never>(() => {}))
    })
    const fresh = await freshStores()
    void start(fresh)
    for (const lang of ['en', 'de'] as const) {
      expect(fresh.session.initialTranslateChoice(lang)).toEqual({ sourceLang: 'fr', targetLang: 'pl' })
    }
  })
})