import Foundation import Testing @testable import EquipoKit /// Paused work (spec 2026-11-24-paused-work-design.md): a held step is /// never a false green. Paul's case: Engineering held for the Claude /// allowance, which resets at 21:00 UTC; Codex has room; noon in Dublin. @MainActor @Suite struct PausedTests { private let dublin = TimeZone(identifier: "Europe/Dublin")! private let britain = Locale(identifier: "en_GB") private let noon = ISO8601DateFormatter().date(from: "2026-11-24T12:00:00Z")! private func task(_ step: String) throws -> ThinTask { let json = """ {"id":"tk1","revision":3,"project_id":"p_1","project":"Equipo","title":"Swipe down to dismiss the keyboard on iOS","brief":"","stage":"in_progress","branch":"","url":"","candidate_sha":"","checks":"","checks_reason":"","verdict":"","review_label":"","needs_you":"","creator":"paul@example.com","created_at":"2026-11-24T09:00:00Z","updated_at":"2026-11-24T09:00:00Z","steps":[\(step)]} """ return try EquipoJSON.decoder.decode(ThinTask.self, from: Data(json.utf8)) } private let held = """ {"id":"s1","specialist":"engineering","state":"queued","reason":"","summary":"","headline":"","verdict":"","review_label":"","checks":"","gate":"","runner_label":"Paul’s Mac mini","hold":{"reason":"headroom","connection":"claude-1","provider":"claude","resets_at":"2026-11-24T21:00:00Z","wake_at":"2026-11-24T21:00:00Z","step_revision":4},"paused":{"since":"2026-11-24T09:00:00Z","used_percent":100,"run_now":{"connection":"cn_codex","provider":"codex","name":"Codex","billing":"subscription"}}} """ @Test func paulsCaseIsPausedNotGreen() throws { let t = try task(held) #expect(t.steps?.first?.hold?.stepRevision == 4, "the hold decodes") let pause = try #require(TaskPause(t, mine: true, now: noon, zone: dublin, locale: britain)) #expect(pause.tone == .paused) #expect(pause.stop == "Paused") #expect(pause.strong == "Paused until 21:00 tonight") #expect(pause.rest == " (in 9 hours): your Claude allowance has run out.") #expect(pause.next == "It carries on by itself then.") #expect(pause.runNow == TaskPause.RunNow(label: "Run it now with Codex", step: "s1", revision: 4, offer: "cn_codex")) #expect(pause.note == "Codex has room now") var report = TaskReport(t, needsYou: false, mine: true, now: noon) report.pause = pause let current = try #require(report.track.first { $0.state == .current }) #expect(current.tone == .paused && current.label == "Paused" && current.stage == .inProgress) } @Test func someoneElseIsNotOfferedRunNow() throws { let pause = try #require(TaskPause(try task(held), mine: false, now: noon, zone: dublin, locale: britain)) #expect(pause.runNow == nil && pause.strong == "Paused until 21:00 tonight") } @Test func theDevicesZoneMovesTheWords() throws { let madrid = TimeZone(identifier: "Europe/Madrid")! let pause = try #require(TaskPause(try task(held), mine: true, now: noon, zone: madrid, locale: britain)) #expect(pause.strong == "Paused until 22:00 tonight") } @Test func aRunningStepIsNotPaused() throws { let running = """ {"id":"s1","specialist":"engineering","state":"running","reason":"","summary":"","headline":"","verdict":"","review_label":"","checks":"","gate":""} """ #expect(TaskPause(try task(running), mine: true, now: noon, zone: dublin, locale: britain) == nil) let report = TaskReport(try task(running), needsYou: false, mine: true, now: noon) #expect(report.track.first { $0.state == .current }?.label == nil) } @Test func otherReasonsKeepTheColourRule() throws { let busy = held.replacingOccurrences(of: "\"reason\":\"headroom\"", with: "\"reason\":\"busy\"").replacingOccurrences(of: "\"provider\":\"claude\"", with: "\"provider\":\"tensorx\"") let b = try #require(TaskPause(try task(busy), mine: true, now: noon, zone: dublin, locale: britain)) #expect(b.tone == .paused && b.strong == "Paused for a moment:" && b.rest == " TensorX is busy with another step for you.") let unknown = held.replacingOccurrences(of: "\"reason\":\"headroom\"", with: "\"reason\":\"spend_unknown\"").replacingOccurrences(of: "\"provider\":\"claude\"", with: "\"provider\":\"tensorx\"") let u = try #require(TaskPause(try task(unknown), mine: true, now: noon, zone: dublin, locale: britain)) #expect(u.tone == .warning && u.stop == "Needs you" && u.strong == "Paused:") let failed = """ {"id":"s1","specialist":"engineering","state":"failed","reason":"push_failed","summary":"","headline":"","verdict":"","review_label":"","checks":"","gate":""} """ let f = try #require(TaskPause(try task(failed), mine: true, now: noon, zone: dublin, locale: britain)) #expect(f.tone == .negative && f.stop == "Stopped") } @Test func aRunnerTooOldWaitsForItsUpdate() throws { let old = """ {"id":"s1","specialist":"engineering","state":"failed","reason":"runner_outdated","summary":"","headline":"","verdict":"","review_label":"","checks":"","gate":"","runner_label":"DOCS-T1"} """ let p = try #require(TaskPause(try task(old), mine: true, now: noon, zone: dublin, locale: britain)) #expect(p.tone == .warning && p.stop == "Needs you" && p.strong == "Waiting for an update:" && p.rest == " the runner on DOCS-T1 is too old for this step.") #expect(p.next == "It carries on by itself once the runner is updated.") } @Test func anOlderRunnerGetsPlainRunNow() throws { let plain = held.replacingOccurrences(of: "\"run_now\":{\"connection\":\"cn_codex\",\"provider\":\"codex\",\"name\":\"Codex\",\"billing\":\"subscription\"}", with: "\"run_now\":{\"plain\":true,\"may_bill\":true}") let p = try #require(TaskPause(try task(plain), mine: true, now: noon, zone: dublin, locale: britain)) #expect(p.runNow?.label == "Run it now anyway" && p.runNow?.offer == "plain" && p.note == "This may use a plan that bills per use.") } @Test func anOfflineRunnerIsNamed() throws { let offline = """ {"id":"s1","specialist":"engineering","state":"queued","reason":"","summary":"","headline":"","verdict":"","review_label":"","checks":"","gate":"","runner_label":"DOCS-T1","paused":{"offline":true,"since":"2026-11-24T11:20:00Z"}} """ let p = try #require(TaskPause(try task(offline), mine: true, now: noon, zone: dublin, locale: britain)) #expect(p.strong == "Paused since 11:20:" && p.next == "It carries on by itself when DOCS-T1 is back online.") } }