|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { buildCronEventPrompt, buildExecEventPrompt } from "./heartbeat-events-filter.js"; |
| 2 | +import { |
| 3 | + buildCronEventPrompt, |
| 4 | + buildExecEventPrompt, |
| 5 | + isCronSystemEvent, |
| 6 | + isExecCompletionEvent, |
| 7 | +} from "./heartbeat-events-filter.js"; |
3 | 8 |
|
4 | 9 | describe("heartbeat event prompts", () => { |
5 | | - it("builds user-relay cron prompt by default", () => { |
6 | | - const prompt = buildCronEventPrompt(["Cron: rotate logs"]); |
7 | | - expect(prompt).toContain("Please relay this reminder to the user"); |
| 10 | + it.each([ |
| 11 | + { |
| 12 | + name: "builds user-relay cron prompt by default", |
| 13 | + events: ["Cron: rotate logs"], |
| 14 | + expected: ["Cron: rotate logs", "Please relay this reminder to the user"], |
| 15 | + unexpected: ["Handle this reminder internally", "Reply HEARTBEAT_OK."], |
| 16 | + }, |
| 17 | + { |
| 18 | + name: "builds internal-only cron prompt when delivery is disabled", |
| 19 | + events: ["Cron: rotate logs"], |
| 20 | + opts: { deliverToUser: false }, |
| 21 | + expected: ["Cron: rotate logs", "Handle this reminder internally"], |
| 22 | + unexpected: ["Please relay this reminder to the user"], |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "falls back to bare heartbeat reply when cron content is empty", |
| 26 | + events: ["", " "], |
| 27 | + expected: ["Reply HEARTBEAT_OK."], |
| 28 | + unexpected: ["Handle this reminder internally"], |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "uses internal empty-content fallback when delivery is disabled", |
| 32 | + events: ["", " "], |
| 33 | + opts: { deliverToUser: false }, |
| 34 | + expected: ["Handle this internally", "HEARTBEAT_OK when nothing needs user-facing follow-up"], |
| 35 | + unexpected: ["Please relay this reminder to the user"], |
| 36 | + }, |
| 37 | + ])("$name", ({ events, opts, expected, unexpected }) => { |
| 38 | + const prompt = buildCronEventPrompt(events, opts); |
| 39 | + for (const part of expected) { |
| 40 | + expect(prompt).toContain(part); |
| 41 | + } |
| 42 | + for (const part of unexpected) { |
| 43 | + expect(prompt).not.toContain(part); |
| 44 | + } |
8 | 45 | }); |
9 | 46 |
|
10 | | - it("builds internal-only cron prompt when delivery is disabled", () => { |
11 | | - const prompt = buildCronEventPrompt(["Cron: rotate logs"], { deliverToUser: false }); |
12 | | - expect(prompt).toContain("Handle this reminder internally"); |
13 | | - expect(prompt).not.toContain("Please relay this reminder to the user"); |
| 47 | + it.each([ |
| 48 | + { |
| 49 | + name: "builds user-relay exec prompt by default", |
| 50 | + opts: undefined, |
| 51 | + expected: ["Please relay the command output to the user", "If it failed"], |
| 52 | + unexpected: ["Handle the result internally"], |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "builds internal-only exec prompt when delivery is disabled", |
| 56 | + opts: { deliverToUser: false }, |
| 57 | + expected: ["Handle the result internally"], |
| 58 | + unexpected: ["Please relay the command output to the user"], |
| 59 | + }, |
| 60 | + ])("$name", ({ opts, expected, unexpected }) => { |
| 61 | + const prompt = buildExecEventPrompt(opts); |
| 62 | + for (const part of expected) { |
| 63 | + expect(prompt).toContain(part); |
| 64 | + } |
| 65 | + for (const part of unexpected) { |
| 66 | + expect(prompt).not.toContain(part); |
| 67 | + } |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe("heartbeat event classification", () => { |
| 72 | + it.each([ |
| 73 | + { value: "exec finished: ok", expected: true }, |
| 74 | + { value: "Exec Finished: failed", expected: true }, |
| 75 | + { value: "cron finished", expected: false }, |
| 76 | + ])("classifies exec completion events for %j", ({ value, expected }) => { |
| 77 | + expect(isExecCompletionEvent(value)).toBe(expected); |
14 | 78 | }); |
15 | 79 |
|
16 | | - it("builds internal-only exec prompt when delivery is disabled", () => { |
17 | | - const prompt = buildExecEventPrompt({ deliverToUser: false }); |
18 | | - expect(prompt).toContain("Handle the result internally"); |
19 | | - expect(prompt).not.toContain("Please relay the command output to the user"); |
| 80 | + it.each([ |
| 81 | + { value: "Cron: rotate logs", expected: true }, |
| 82 | + { value: " Cron: rotate logs ", expected: true }, |
| 83 | + { value: "", expected: false }, |
| 84 | + { value: " ", expected: false }, |
| 85 | + { value: "HEARTBEAT_OK", expected: false }, |
| 86 | + { value: "heartbeat_ok: already handled", expected: false }, |
| 87 | + { value: "heartbeat poll: noop", expected: false }, |
| 88 | + { value: "heartbeat wake: noop", expected: false }, |
| 89 | + { value: "exec finished: ok", expected: false }, |
| 90 | + ])("classifies cron system events for %j", ({ value, expected }) => { |
| 91 | + expect(isCronSystemEvent(value)).toBe(expected); |
20 | 92 | }); |
21 | 93 | }); |
0 commit comments