|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { render } from "lit"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type { SessionGoal } from "../../api/types.ts"; |
| 6 | +import { i18n } from "../../i18n/index.ts"; |
| 7 | +import { renderChatGoal, clearGoalElapsedTimers } from "./components/chat-composer-goal.ts"; |
| 8 | +import { getChatComposerState, resetChatComposerState } from "./components/chat-composer-state.ts"; |
| 9 | + |
| 10 | +const goal: SessionGoal = { |
| 11 | + schemaVersion: 1, |
| 12 | + id: "goal-timing", |
| 13 | + objective: "Verify the deployment", |
| 14 | + status: "active", |
| 15 | + createdAt: 1_000, |
| 16 | + updatedAt: 61_000, |
| 17 | + tokenStart: 0, |
| 18 | + tokensUsed: 100, |
| 19 | + continuationTurns: 0, |
| 20 | +}; |
| 21 | + |
| 22 | +function mountGoal(initial: SessionGoal) { |
| 23 | + const container = document.createElement("div"); |
| 24 | + document.body.append(container); |
| 25 | + const state = getChatComposerState("goal-timing"); |
| 26 | + const draw = (value: SessionGoal | undefined) => |
| 27 | + render(renderChatGoal(state, value, { canAct: false, requestUpdate: () => {} }), container); |
| 28 | + const part = draw(initial); |
| 29 | + return { |
| 30 | + container, |
| 31 | + draw, |
| 32 | + part, |
| 33 | + elapsed: () => container.querySelector(".agent-chat__goal-elapsed")?.textContent, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe("goal elapsed presentation", () => { |
| 38 | + beforeEach(async () => { |
| 39 | + await i18n.setLocale("en"); |
| 40 | + vi.useFakeTimers(); |
| 41 | + vi.setSystemTime(121_000); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + clearGoalElapsedTimers(); |
| 46 | + resetChatComposerState(); |
| 47 | + document.body.replaceChildren(); |
| 48 | + vi.useRealTimers(); |
| 49 | + }); |
| 50 | + |
| 51 | + it.each([ |
| 52 | + ["active", "2m"], |
| 53 | + ["paused", "1m"], |
| 54 | + ["blocked", "1m"], |
| 55 | + ["usage_limited", "1m"], |
| 56 | + ["budget_limited", "1m"], |
| 57 | + ["complete", "1m"], |
| 58 | + ] as const)("renders elapsed time immediately for %s goals", (status, elapsed) => { |
| 59 | + const view = mountGoal({ ...goal, status }); |
| 60 | + expect(view.elapsed()).toBe(elapsed); |
| 61 | + }); |
| 62 | + |
| 63 | + it("replaces a live tick with the authoritative stop time and resumes ticking", () => { |
| 64 | + const view = mountGoal(goal); |
| 65 | + vi.advanceTimersByTime(1_000); |
| 66 | + expect(view.elapsed()).toBe("2m"); |
| 67 | + |
| 68 | + view.draw({ ...goal, status: "paused", pausedAt: 46_000 }); |
| 69 | + expect(view.elapsed()).toBe("45s"); |
| 70 | + vi.advanceTimersByTime(60_000); |
| 71 | + expect(view.elapsed()).toBe("45s"); |
| 72 | + |
| 73 | + view.draw(goal); |
| 74 | + expect(view.elapsed()).toBe("3m"); |
| 75 | + vi.advanceTimersByTime(60_000); |
| 76 | + expect(view.elapsed()).toBe("4m"); |
| 77 | + |
| 78 | + view.draw({ ...goal, status: "complete", completedAt: 151_000 }); |
| 79 | + expect(view.elapsed()).toBe("2m"); |
| 80 | + vi.advanceTimersByTime(60_000); |
| 81 | + expect(view.elapsed()).toBe("2m"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("retires the active timer when the goal is removed", () => { |
| 85 | + const view = mountGoal(goal); |
| 86 | + expect(vi.getTimerCount()).toBe(1); |
| 87 | + view.draw(undefined); |
| 88 | + expect(view.container.querySelector(".agent-chat__goal")).toBeNull(); |
| 89 | + expect(vi.getTimerCount()).toBe(0); |
| 90 | + }); |
| 91 | + |
| 92 | + it("retires and resumes the active timer with the rendered host connection", () => { |
| 93 | + const view = mountGoal(goal); |
| 94 | + view.part.setConnected(false); |
| 95 | + expect(vi.getTimerCount()).toBe(0); |
| 96 | + vi.advanceTimersByTime(60_000); |
| 97 | + view.part.setConnected(true); |
| 98 | + expect(view.elapsed()).toBe("3m"); |
| 99 | + expect(vi.getTimerCount()).toBe(1); |
| 100 | + }); |
| 101 | +}); |
0 commit comments