|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 4 | + |
| 5 | +import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 6 | +import { |
| 7 | + activityReason, |
| 8 | + afterCheck, |
| 9 | + afterFailure, |
| 10 | + afterProbe, |
| 11 | + AUTO_UPDATE_CHECK_INTERVAL_MS, |
| 12 | + AUTO_UPDATE_FAILURE_BACKOFF_MS, |
| 13 | + AUTO_UPDATE_FIRST_CHECK_DELAY_MS, |
| 14 | + AUTO_UPDATE_MAX_QUIET_WAIT_MS, |
| 15 | + AUTO_UPDATE_QUIET_POLL_MS, |
| 16 | + hookReason, |
| 17 | + initialAutoUpdateState, |
| 18 | + nextAutoUpdateStep, |
| 19 | + offerableVersion, |
| 20 | + readActivity, |
| 21 | +} from "./autoUpdate.ts"; |
| 22 | + |
| 23 | +const quiet = { activeRuns: 0, pendingRequests: 0, waitingLinkRequests: 0, backgroundTasks: 0 }; |
| 24 | + |
| 25 | +describe("offerableVersion", () => { |
| 26 | + it.each([ |
| 27 | + { current: "0.1.9-fork.1", candidate: "0.1.9-fork.2", expected: "0.1.9-fork.2" }, |
| 28 | + { current: "0.1.9-fork.3", candidate: "0.1.10-fork.1", expected: "0.1.10-fork.1" }, |
| 29 | + { current: "0.1.9", candidate: "0.1.10", expected: "0.1.10" }, |
| 30 | + { current: "0.1.9-fork.2", candidate: "0.1.9-fork.2", expected: undefined }, |
| 31 | + { current: "0.1.9-fork.2", candidate: "0.1.9-fork.1", expected: undefined }, |
| 32 | + { current: "0.1.9", candidate: "0.1.10-nightly.20261112.1", expected: undefined }, |
| 33 | + { current: "0.1.9", candidate: "latest", expected: undefined }, |
| 34 | + { current: "0.1.9", candidate: undefined, expected: undefined }, |
| 35 | + ])("$current -> $candidate", ({ current, candidate, expected }) => { |
| 36 | + expect(offerableVersion({ current, candidate, rolledBackVersion: undefined })).toBe(expected); |
| 37 | + }); |
| 38 | + |
| 39 | + it("skips the release the launcher rolled back, but not a newer one", () => { |
| 40 | + expect( |
| 41 | + offerableVersion({ |
| 42 | + current: "0.1.9-fork.1", |
| 43 | + candidate: "0.1.9-fork.2", |
| 44 | + rolledBackVersion: "0.1.9-fork.2", |
| 45 | + }), |
| 46 | + ).toBeUndefined(); |
| 47 | + expect( |
| 48 | + offerableVersion({ |
| 49 | + current: "0.1.9-fork.1", |
| 50 | + candidate: "0.1.9-fork.3", |
| 51 | + rolledBackVersion: "0.1.9-fork.2", |
| 52 | + }), |
| 53 | + ).toBe("0.1.9-fork.3"); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("activityReason", () => { |
| 58 | + it("is null only when nothing is working", () => { |
| 59 | + expect(activityReason(quiet)).toBeNull(); |
| 60 | + expect(activityReason({ ...quiet, activeRuns: 1 })).toBe("1 running turn"); |
| 61 | + expect(activityReason({ ...quiet, pendingRequests: 2, backgroundTasks: 1 })).toBe( |
| 62 | + "2 pending approval or questions, 1 background task", |
| 63 | + ); |
| 64 | + expect(activityReason({ ...quiet, waitingLinkRequests: 1 })).toBe("1 pending link request"); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("hookReason", () => { |
| 69 | + it("allows only a clean exit", () => { |
| 70 | + expect(hookReason({ code: 0, stdout: "ok", stderr: "", timedOut: false })).toBeNull(); |
| 71 | + expect( |
| 72 | + hookReason({ code: 1, stdout: "\n deploy running \nmore", stderr: "", timedOut: false }), |
| 73 | + ).toBe("the quiet hook said not now: deploy running"); |
| 74 | + expect(hookReason({ code: 3, stdout: "", stderr: "busy", timedOut: false })).toBe( |
| 75 | + "the quiet hook said not now: busy", |
| 76 | + ); |
| 77 | + expect(hookReason({ code: 1, stdout: "", stderr: "", timedOut: false })).toBe( |
| 78 | + "the quiet hook said not now", |
| 79 | + ); |
| 80 | + expect(hookReason({ code: null, stdout: "", stderr: "", timedOut: true })).toBe( |
| 81 | + "the quiet hook did not answer in time", |
| 82 | + ); |
| 83 | + expect(hookReason({ error: "ProcessSpawnError" })).toBe( |
| 84 | + "the quiet hook could not run (ProcessSpawnError)", |
| 85 | + ); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("auto-update schedule", () => { |
| 90 | + const start = 1_000_000; |
| 91 | + |
| 92 | + it("checks first after the startup delay, then hourly when nothing is newer", () => { |
| 93 | + const state = initialAutoUpdateState(start); |
| 94 | + expect(nextAutoUpdateStep(state, start)).toEqual({ type: "none" }); |
| 95 | + const due = start + AUTO_UPDATE_FIRST_CHECK_DELAY_MS; |
| 96 | + expect(nextAutoUpdateStep(state, due)).toEqual({ type: "check" }); |
| 97 | + const after = afterCheck(due, undefined); |
| 98 | + expect(after).toEqual({ phase: "idle", nextCheckAt: due + AUTO_UPDATE_CHECK_INTERVAL_MS }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("waits for quiet once a minute, then updates", () => { |
| 102 | + const waiting = afterCheck(start, "0.1.9-fork.2"); |
| 103 | + expect(nextAutoUpdateStep(waiting, start)).toEqual({ type: "probe", version: "0.1.9-fork.2" }); |
| 104 | + if (waiting.phase !== "waiting") throw new Error("expected waiting"); |
| 105 | + const busy = afterProbe(waiting, start, "1 running turn"); |
| 106 | + expect(busy.outcome).toBe("wait"); |
| 107 | + expect(nextAutoUpdateStep(busy.state, start + 1)).toEqual({ type: "none" }); |
| 108 | + expect(nextAutoUpdateStep(busy.state, start + AUTO_UPDATE_QUIET_POLL_MS)).toEqual({ |
| 109 | + type: "probe", |
| 110 | + version: "0.1.9-fork.2", |
| 111 | + }); |
| 112 | + if (busy.state.phase !== "waiting") throw new Error("expected waiting"); |
| 113 | + expect(afterProbe(busy.state, start + AUTO_UPDATE_QUIET_POLL_MS, null).outcome).toBe("update"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("never forces: a bounded wait gives up until the next check", () => { |
| 117 | + const waiting = afterCheck(start, "0.1.9-fork.2"); |
| 118 | + if (waiting.phase !== "waiting") throw new Error("expected waiting"); |
| 119 | + const late = start + AUTO_UPDATE_MAX_QUIET_WAIT_MS; |
| 120 | + const gaveUp = afterProbe(waiting, late, "1 running turn"); |
| 121 | + expect(gaveUp.outcome).toBe("give-up"); |
| 122 | + expect(gaveUp.state).toEqual({ |
| 123 | + phase: "idle", |
| 124 | + nextCheckAt: late + AUTO_UPDATE_CHECK_INTERVAL_MS, |
| 125 | + }); |
| 126 | + // Quiet at the bound still updates. |
| 127 | + expect(afterProbe(waiting, late, null).outcome).toBe("update"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("backs off after a failed install, longer than a normal check", () => { |
| 131 | + const failed = afterFailure(start); |
| 132 | + expect(failed).toEqual({ phase: "idle", nextCheckAt: start + AUTO_UPDATE_FAILURE_BACKOFF_MS }); |
| 133 | + expect(AUTO_UPDATE_FAILURE_BACKOFF_MS).toBeGreaterThan(AUTO_UPDATE_CHECK_INTERVAL_MS); |
| 134 | + expect(nextAutoUpdateStep(failed, start + AUTO_UPDATE_CHECK_INTERVAL_MS)).toEqual({ |
| 135 | + type: "none", |
| 136 | + }); |
| 137 | + expect(nextAutoUpdateStep(failed, start + AUTO_UPDATE_FAILURE_BACKOFF_MS)).toEqual({ |
| 138 | + type: "check", |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe("readActivity", () => { |
| 144 | + it.effect("counts live work on threads that still exist", () => |
| 145 | + Effect.gen(function* () { |
| 146 | + const sql = yield* SqlClient.SqlClient; |
| 147 | + expect(yield* readActivity(sql)).toEqual(quiet); |
| 148 | + |
| 149 | + const now = "2026-11-12T00:00:00.000Z"; |
| 150 | + const thread = (id: string, deletedAt: string | null) => sql` |
| 151 | + INSERT INTO orchestration_v2_projection_threads |
| 152 | + (thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, |
| 153 | + created_at, updated_at, deleted_at, payload_json) |
| 154 | + VALUES (${id}, 'project', 'title', 'codex', 'full-access', 'default', |
| 155 | + ${now}, ${now}, ${deletedAt}, '{}') |
| 156 | + `; |
| 157 | + yield* thread("live", null); |
| 158 | + yield* thread("gone", now); |
| 159 | + const run = (id: string, threadId: string, ordinal: number, status: string) => sql` |
| 160 | + INSERT INTO orchestration_v2_projection_runs |
| 161 | + (run_id, thread_id, ordinal, provider, status, requested_at, payload_json) |
| 162 | + VALUES (${id}, ${threadId}, ${ordinal}, 'codex', ${status}, ${now}, '{}') |
| 163 | + `; |
| 164 | + yield* run("queued", "live", 1, "queued"); |
| 165 | + yield* run("done", "live", 2, "completed"); |
| 166 | + yield* run("ghost", "gone", 1, "running"); |
| 167 | + expect(yield* readActivity(sql)).toEqual(quiet); |
| 168 | + |
| 169 | + yield* run("running", "live", 3, "running"); |
| 170 | + yield* sql` |
| 171 | + INSERT INTO orchestration_v2_projection_runtime_requests |
| 172 | + (runtime_request_id, thread_id, node_id, kind, status, created_at, payload_json) |
| 173 | + VALUES ('request', 'live', 'node', 'approval', 'pending', ${now}, '{}') |
| 174 | + `; |
| 175 | + yield* sql` |
| 176 | + INSERT INTO orchestration_v2_projection_turn_items |
| 177 | + (turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json) |
| 178 | + VALUES ('link', 'live', 1, 'link_request', 'waiting', ${now}, '{}') |
| 179 | + `; |
| 180 | + yield* sql` |
| 181 | + INSERT INTO orchestration_v2_projection_provider_threads |
| 182 | + (provider_thread_id, thread_id, provider, status, updated_at, payload_json) |
| 183 | + VALUES ('provider', 'live', 'codex', 'idle', ${now}, |
| 184 | + '{"pendingBackgroundTasks":[{"id":"a"},{"id":"b"}]}') |
| 185 | + `; |
| 186 | + expect(yield* readActivity(sql)).toEqual({ |
| 187 | + activeRuns: 1, |
| 188 | + pendingRequests: 1, |
| 189 | + waitingLinkRequests: 1, |
| 190 | + backgroundTasks: 2, |
| 191 | + }); |
| 192 | + }).pipe(Effect.provide(SqlitePersistence.layerMemory)), |
| 193 | + ); |
| 194 | +}); |
0 commit comments