import Foundation import Testing @testable import T3Code @Suite("Web V2 home thread metadata") struct HomeThreadMetadataTests { private let now = Date(timeIntervalSince1970: 10_000) @Test func statusLabelsFollowTheWebV2RowVocabulary() { let expected: [(FeatureThreadState, HomeThreadStatus, String?)] = [ (.idle, .ready, nil), (.queued, .working, "Working"), (.working, .working, "Working"), (.waitingForApproval, .approval, "Approval"), (.waitingForInput, .input, "Input"), (.failed, .failed, "Failed"), (.completed, .done, "Done"), ] for (state, status, label) in expected { let thread = FeatureThread( id: state.rawValue, projectID: "project", title: "Task", state: state ) #expect(thread.homeStatus == status) #expect(thread.homeStatusLabel == label) } } @Test func workingDurationMatchesTheCompactWebFormatAndClampsFutureDates() { let thread = FeatureThread( id: "working", projectID: "project", title: "Build", state: .working, workingStartedAt: now.addingTimeInterval(-5_465) ) let future = FeatureThread( id: "queued", projectID: "project", title: "Queue", state: .queued, workingStartedAt: now.addingTimeInterval(5) ) let idle = FeatureThread( id: "idle", projectID: "project", title: "Rest", state: .idle, workingStartedAt: now.addingTimeInterval(-10) ) #expect(thread.homeWorkingDuration(at: now) == "1h 31m") #expect(future.homeWorkingDuration(at: now) == "0s") #expect(idle.homeWorkingDuration(at: now) == nil) } @Test func rowAttributionPrefersCurrentEnvironmentNameAndWireProviderName() { let thread = FeatureThread( id: "thread", projectID: "project", environmentID: "device", environmentName: "Old device name", title: "Build", branch: "feat/web-v2-home", worktreePath: "/worktrees/web-v2-home", providerID: "codex-work", providerName: "Codex Work" ) let snapshot = FeatureSnapshot( environments: [ FeatureEnvironment( id: "device", name: "leftbook", endpoint: "https://leftbook.example" ), ], projects: [ FeatureProject( id: "project", environmentID: "device", name: "t3code", path: "/work/t3code" ), ], providers: [FeatureProvider(id: "codex-work", name: "Config name")] ) #expect(thread.homeEnvironmentLabel(in: snapshot) == "leftbook") #expect(thread.homeProviderLabel(in: snapshot) == "Codex Work") #expect(thread.branch == "feat/web-v2-home") #expect(thread.worktreePath == "/worktrees/web-v2-home") } @Test func rowAttributionFallsBackThroughProjectAndProviderCatalog() { let thread = FeatureThread( id: "thread", projectID: "project", title: "Build", providerID: "claude" ) let snapshot = FeatureSnapshot( environments: [ FeatureEnvironment( id: "device", name: "steambox", endpoint: "https://steambox.example" ), ], projects: [ FeatureProject( id: "project", environmentID: "device", name: "t3code", path: "/work/t3code" ), ], providers: [FeatureProvider(id: "claude", name: "Claude")] ) #expect(thread.homeEnvironmentLabel(in: snapshot) == "steambox") #expect(thread.homeProviderLabel(in: snapshot) == "Claude") } @Test func rowContextCarriesHarnessIdentityAndCustomProviderFallback() throws { let knownThread = FeatureThread( id: "known", projectID: "project", title: "Use Claude", providerID: "work-claude" ) let customThread = FeatureThread( id: "custom", projectID: "project", title: "Use a custom harness", providerID: "acme-agent", providerName: "Acme Agent" ) let snapshot = FeatureSnapshot( projects: [ FeatureProject( id: "project", environmentID: "device", name: "t3code", path: "/work/t3code" ), ], threads: [knownThread, customThread], providers: [ FeatureProvider(id: "work-claude", name: "Claude Code", driver: "custom"), FeatureProvider(id: "acme-agent", name: "Acme Agent", driver: "custom"), ] ) let contexts = HomeThreadRowContext.index(snapshot: snapshot) let known = try #require(contexts[knownThread.id]) let custom = try #require(contexts[customThread.id]) #expect(known.providerID == "work-claude") #expect(known.providerDriver == "custom") #expect(known.providerName == "Claude Code") #expect( ProviderBrand.resolve( driver: known.providerDriver, providerID: known.providerID, providerName: known.providerName ) == .claude ) #expect(custom.providerID == "acme-agent") #expect(custom.providerDriver == "custom") #expect(custom.providerName == "Acme Agent") #expect( ProviderBrand.resolve( driver: custom.providerDriver, providerID: custom.providerID, providerName: custom.providerName ) == nil ) } }