|
| 1 | +// End-to-end regression: create a PersonaPack with a scheduled persona, |
| 2 | +// verify the controller stamps an Instance + Schedule, force the schedule |
| 3 | +// to trigger immediately (by clearing its lastRunTime so the cron |
| 4 | +// scheduler computes "next run" in the past), and assert that a real |
| 5 | +// AgentRun gets created, completes, and produces a substantive response |
| 6 | +// in the UX. |
| 7 | +// |
| 8 | +// This is the regression guard for the "ghost run" bug where the |
| 9 | +// scheduler was silently claiming success due to run-name collisions |
| 10 | +// after a PersonaPack disable/re-enable cycle. If that bug ever comes |
| 11 | +// back, this test fails because no new AgentRun will actually appear. |
| 12 | + |
| 13 | +const PACK = `cy-ppsched-${Date.now()}`; |
| 14 | +const PERSONA = "analyst"; |
| 15 | +const INSTANCE = `${PACK}-${PERSONA}`; |
| 16 | +const SCHEDULE = `${INSTANCE}-schedule`; |
| 17 | + |
| 18 | +function authHeaders(): Record<string, string> { |
| 19 | + const token = Cypress.env("API_TOKEN"); |
| 20 | + const h: Record<string, string> = { "Content-Type": "application/json" }; |
| 21 | + if (token) h["Authorization"] = `Bearer ${token}`; |
| 22 | + return h; |
| 23 | +} |
| 24 | + |
| 25 | +describe("PersonaPack — scheduled run fires and produces a response", () => { |
| 26 | + after(() => { |
| 27 | + cy.deletePersonaPack(PACK); |
| 28 | + cy.deleteInstance(INSTANCE); |
| 29 | + // The schedule + runs are owned by the pack/instance and should GC, |
| 30 | + // but clean up defensively in case of leftover resources. |
| 31 | + cy.exec( |
| 32 | + `kubectl delete sympoziumschedule ${SCHEDULE} -n default --ignore-not-found --wait=false`, |
| 33 | + { failOnNonZeroExit: false }, |
| 34 | + ); |
| 35 | + cy.exec( |
| 36 | + `kubectl delete agentrun -n default -l sympozium.ai/schedule=${SCHEDULE} --ignore-not-found --wait=false`, |
| 37 | + { failOnNonZeroExit: false }, |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it("stamps resources, triggers the schedule immediately, and renders the run's response", () => { |
| 42 | + // ── Step 1: create a PersonaPack with a scheduled persona ────────────── |
| 43 | + // Use a cron that only fires hourly so the test controls timing |
| 44 | + // (otherwise the schedule might fire on its natural cadence during |
| 45 | + // the test and create a confusing duplicate). We'll force-trigger |
| 46 | + // the initial run via status patch below. |
| 47 | + const manifest = `apiVersion: sympozium.ai/v1alpha1 |
| 48 | +kind: PersonaPack |
| 49 | +metadata: |
| 50 | + name: ${PACK} |
| 51 | + namespace: default |
| 52 | +spec: |
| 53 | + enabled: true |
| 54 | + description: cypress scheduled-run regression guard |
| 55 | + category: test |
| 56 | + version: "0.0.1" |
| 57 | + baseURL: http://host.docker.internal:1234/v1 |
| 58 | + authRefs: |
| 59 | + - provider: lm-studio |
| 60 | + secret: "" |
| 61 | + personas: |
| 62 | + - name: ${PERSONA} |
| 63 | + displayName: Cypress Analyst |
| 64 | + systemPrompt: You are a precise echo service. When asked to reply with a specific string, reply with exactly that string and nothing else. |
| 65 | + model: qwen/qwen3.5-9b |
| 66 | + schedule: |
| 67 | + type: scheduled |
| 68 | + cron: "0 * * * *" |
| 69 | + task: "Reply with exactly this sentinel and nothing else: SCHEDULED_SENTINEL_319. Do not use any tools." |
| 70 | +`; |
| 71 | + cy.writeFile(`cypress/tmp/${PACK}.yaml`, manifest); |
| 72 | + cy.exec(`kubectl apply -f cypress/tmp/${PACK}.yaml`); |
| 73 | + |
| 74 | + // ── Step 2: wait for the stamped Instance and Schedule to appear ─────── |
| 75 | + cy.visit("/instances"); |
| 76 | + cy.contains(INSTANCE, { timeout: 60000 }).should("be.visible"); |
| 77 | + |
| 78 | + cy.visit("/schedules"); |
| 79 | + cy.contains(SCHEDULE, { timeout: 30000 }).should("exist"); |
| 80 | + |
| 81 | + // ── Step 3: force-trigger the schedule by clearing lastRunTime ───────── |
| 82 | + // The scheduler computes `nextRun = sched.Next(lastRun)`. When |
| 83 | + // lastRunTime is unset, it uses `creationTimestamp - 24h`, so the |
| 84 | + // next computed cron tick will be in the past and the reconcile |
| 85 | + // fires a run immediately. |
| 86 | + // |
| 87 | + // We retry because the initial status may be empty right after the |
| 88 | + // controller creates the schedule. |
| 89 | + cy.exec( |
| 90 | + `for i in $(seq 1 10); do ` + |
| 91 | + `if kubectl patch sympoziumschedule ${SCHEDULE} -n default ` + |
| 92 | + `--subresource=status --type=json ` + |
| 93 | + `-p='[{"op":"remove","path":"/status/lastRunTime"}]' 2>/dev/null; then ` + |
| 94 | + ` echo patched; exit 0; fi; ` + |
| 95 | + `sleep 2; done`, |
| 96 | + { failOnNonZeroExit: false }, |
| 97 | + ); |
| 98 | + |
| 99 | + // ── Step 4: wait for the scheduler to create an AgentRun ─────────────── |
| 100 | + // The reconciler should pick up the cleared status within a few |
| 101 | + // seconds and create a run with a real, unused numeric suffix — |
| 102 | + // NOT silently reuse an existing one. |
| 103 | + let runName = ""; |
| 104 | + cy.then(() => { |
| 105 | + const deadline = Date.now() + 60000; |
| 106 | + const retry = (): Cypress.Chainable<unknown> => { |
| 107 | + if (Date.now() > deadline) { |
| 108 | + throw new Error( |
| 109 | + `no AgentRun appeared for schedule ${SCHEDULE} within 60s`, |
| 110 | + ); |
| 111 | + } |
| 112 | + return cy |
| 113 | + .request({ |
| 114 | + url: `/api/v1/runs?namespace=default`, |
| 115 | + headers: authHeaders(), |
| 116 | + }) |
| 117 | + .then((resp) => { |
| 118 | + // /api/v1/runs returns a bare array, not {items:[...]}. |
| 119 | + const all = Array.isArray(resp.body) |
| 120 | + ? (resp.body as Array<{ |
| 121 | + metadata: { |
| 122 | + name: string; |
| 123 | + labels?: Record<string, string>; |
| 124 | + creationTimestamp: string; |
| 125 | + }; |
| 126 | + }>) |
| 127 | + : []; |
| 128 | + const runs = all |
| 129 | + .filter( |
| 130 | + (r) => |
| 131 | + r.metadata?.labels?.["sympozium.ai/schedule"] === SCHEDULE, |
| 132 | + ) |
| 133 | + .sort((a, b) => |
| 134 | + b.metadata.creationTimestamp.localeCompare( |
| 135 | + a.metadata.creationTimestamp, |
| 136 | + ), |
| 137 | + ); |
| 138 | + if (runs.length > 0) { |
| 139 | + runName = runs[0].metadata.name; |
| 140 | + return cy.wrap(runName); |
| 141 | + } |
| 142 | + cy.wait(2000, { log: false }); |
| 143 | + return retry(); |
| 144 | + }); |
| 145 | + }; |
| 146 | + return retry(); |
| 147 | + }); |
| 148 | + |
| 149 | + // ── Step 5: wait for the run to finish and assert Succeeded ──────────── |
| 150 | + cy.then(() => cy.waitForRunTerminal(runName, 6 * 60 * 1000)); |
| 151 | + cy.then(() => |
| 152 | + cy |
| 153 | + .request({ |
| 154 | + url: `/api/v1/runs/${runName}?namespace=default`, |
| 155 | + headers: authHeaders(), |
| 156 | + }) |
| 157 | + .then((resp) => { |
| 158 | + const phase = resp.body?.status?.phase as string; |
| 159 | + const err = resp.body?.status?.error as string | undefined; |
| 160 | + expect( |
| 161 | + phase, |
| 162 | + `run ${runName} should have Succeeded (error: ${err || "n/a"})`, |
| 163 | + ).to.eq("Succeeded"); |
| 164 | + }), |
| 165 | + ); |
| 166 | + |
| 167 | + // ── Step 6: open the run detail and verify the response is real ──────── |
| 168 | + cy.then(() => cy.visit(`/runs/${runName}`)); |
| 169 | + cy.contains("Succeeded", { timeout: 20000 }).should("be.visible"); |
| 170 | + cy.contains("button", "Result", { timeout: 20000 }).click({ force: true }); |
| 171 | + |
| 172 | + // Deterministic assertion: the scheduled run MUST contain the tool's |
| 173 | + // sentinel output in its response. This proves end-to-end that: |
| 174 | + // (a) the schedule fired a REAL run (not a ghost from name collision) |
| 175 | + // (b) the run actually reached the provider |
| 176 | + // (c) the provider executed the tool call |
| 177 | + // (d) the tool's output was surfaced in the response |
| 178 | + cy.contains("No result available").should("not.exist"); |
| 179 | + cy.get("[role='tabpanel']", { timeout: 20000 }) |
| 180 | + .invoke("text") |
| 181 | + .then((raw) => { |
| 182 | + expect( |
| 183 | + raw, |
| 184 | + "scheduled run must contain the tool's sentinel output", |
| 185 | + ).to.include("SCHEDULED_SENTINEL_319"); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +export {}; |
0 commit comments