import assert from 'node:assert/strict'
import test from 'node:test'
import { createWorkspaceRefreshGate } from '../src/app/workspace-refresh-gate.ts'

test('a newer full workspace refresh aborts and invalidates the previous generation', () => {
  const gate = createWorkspaceRefreshGate()
  const first = gate.begin()
  const second = gate.begin()

  assert.equal(first.signal.aborted, true)
  assert.equal(first.isCurrent(), false)
  assert.equal(second.signal.aborted, false)
  assert.equal(second.isCurrent(), true)
})

test('a targeted origin-project refresh also supersedes an older full refresh', () => {
  const gate = createWorkspaceRefreshGate()
  const fullRefresh = gate.begin()
  const originRefresh = gate.begin()

  assert.equal(fullRefresh.signal.aborted, true)
  assert.equal(fullRefresh.isCurrent(), false)
  assert.equal(originRefresh.isCurrent(), true)
})

test('invalidating on session cleanup prevents stale workspace commits', () => {
  const gate = createWorkspaceRefreshGate()
  const request = gate.begin()

  gate.invalidate()

  assert.equal(request.signal.aborted, true)
  assert.equal(request.isCurrent(), false)
})
