|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + PostHogAPIClient, |
| 4 | + TaskAutomationValidationError, |
| 5 | +} from "./posthog-client"; |
| 6 | + |
| 7 | +const automationPayload = { |
| 8 | + id: "automation-1", |
| 9 | + name: "Daily PRs", |
| 10 | + prompt: "Check PRs", |
| 11 | + repository: "posthog/posthog", |
| 12 | + github_integration: 7, |
| 13 | + cron_expression: "0 9 * * *", |
| 14 | + timezone: "Europe/London", |
| 15 | + template_id: "llm-skill:daily-prs", |
| 16 | + enabled: true, |
| 17 | + last_run_at: null, |
| 18 | + last_run_status: null, |
| 19 | + last_task_id: null, |
| 20 | + last_task_run_id: null, |
| 21 | + last_error: null, |
| 22 | + created_at: "2026-07-21T00:00:00Z", |
| 23 | + updated_at: "2026-07-21T00:00:00Z", |
| 24 | +}; |
| 25 | + |
| 26 | +function jsonResponse(body: unknown, status = 200): Response { |
| 27 | + return new Response(JSON.stringify(body), { |
| 28 | + status, |
| 29 | + headers: { "Content-Type": "application/json" }, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +describe("PostHogAPIClient task automations", () => { |
| 34 | + const fetch = vi.fn(); |
| 35 | + const client = new PostHogAPIClient( |
| 36 | + "https://app.posthog.test", |
| 37 | + async () => "access-token", |
| 38 | + async () => "refreshed-token", |
| 39 | + 42, |
| 40 | + { appVersion: "test", fetch }, |
| 41 | + ); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + fetch.mockReset(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("lists automations and normalizes optional response fields", async () => { |
| 48 | + const minimalPayload = { |
| 49 | + ...automationPayload, |
| 50 | + github_integration: undefined, |
| 51 | + timezone: undefined, |
| 52 | + template_id: undefined, |
| 53 | + enabled: undefined, |
| 54 | + }; |
| 55 | + fetch.mockResolvedValueOnce( |
| 56 | + jsonResponse({ |
| 57 | + count: 1, |
| 58 | + next: null, |
| 59 | + previous: null, |
| 60 | + results: [minimalPayload], |
| 61 | + }), |
| 62 | + ); |
| 63 | + |
| 64 | + await expect(client.listTaskAutomations()).resolves.toEqual([ |
| 65 | + expect.objectContaining({ |
| 66 | + id: "automation-1", |
| 67 | + github_integration: null, |
| 68 | + timezone: null, |
| 69 | + template_id: null, |
| 70 | + enabled: true, |
| 71 | + }), |
| 72 | + ]); |
| 73 | + expect(fetch).toHaveBeenCalledWith( |
| 74 | + new URL( |
| 75 | + "https://app.posthog.test/api/projects/42/task_automations/?limit=500", |
| 76 | + ), |
| 77 | + expect.objectContaining({ method: "GET" }), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("gets and creates automations through generated endpoints", async () => { |
| 82 | + fetch |
| 83 | + .mockResolvedValueOnce(jsonResponse(automationPayload)) |
| 84 | + .mockResolvedValueOnce(jsonResponse(automationPayload, 201)); |
| 85 | + |
| 86 | + await expect(client.getTaskAutomation("automation-1")).resolves.toEqual( |
| 87 | + automationPayload, |
| 88 | + ); |
| 89 | + await expect( |
| 90 | + client.createTaskAutomation({ |
| 91 | + name: "Daily PRs", |
| 92 | + prompt: "Check PRs", |
| 93 | + repository: "posthog/posthog", |
| 94 | + github_integration: 7, |
| 95 | + cron_expression: "0 9 * * *", |
| 96 | + timezone: "Europe/London", |
| 97 | + template_id: "llm-skill:daily-prs", |
| 98 | + enabled: true, |
| 99 | + }), |
| 100 | + ).resolves.toEqual(automationPayload); |
| 101 | + |
| 102 | + expect(fetch).toHaveBeenNthCalledWith( |
| 103 | + 2, |
| 104 | + new URL("https://app.posthog.test/api/projects/42/task_automations/"), |
| 105 | + expect.objectContaining({ |
| 106 | + method: "POST", |
| 107 | + body: JSON.stringify({ |
| 108 | + name: "Daily PRs", |
| 109 | + prompt: "Check PRs", |
| 110 | + repository: "posthog/posthog", |
| 111 | + github_integration: 7, |
| 112 | + cron_expression: "0 9 * * *", |
| 113 | + timezone: "Europe/London", |
| 114 | + template_id: "llm-skill:daily-prs", |
| 115 | + enabled: true, |
| 116 | + }), |
| 117 | + }), |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("updates, deletes, and runs automations", async () => { |
| 122 | + fetch |
| 123 | + .mockResolvedValueOnce( |
| 124 | + jsonResponse({ ...automationPayload, enabled: false }), |
| 125 | + ) |
| 126 | + .mockResolvedValueOnce(new Response(null, { status: 204 })) |
| 127 | + .mockResolvedValueOnce(jsonResponse(automationPayload)); |
| 128 | + |
| 129 | + await expect( |
| 130 | + client.updateTaskAutomation("automation-1", { enabled: false }), |
| 131 | + ).resolves.toMatchObject({ enabled: false }); |
| 132 | + await expect( |
| 133 | + client.deleteTaskAutomation("automation-1"), |
| 134 | + ).resolves.toBeUndefined(); |
| 135 | + await expect(client.runTaskAutomation("automation-1")).resolves.toEqual( |
| 136 | + automationPayload, |
| 137 | + ); |
| 138 | + |
| 139 | + expect(fetch).toHaveBeenNthCalledWith( |
| 140 | + 1, |
| 141 | + new URL( |
| 142 | + "https://app.posthog.test/api/projects/42/task_automations/automation-1/", |
| 143 | + ), |
| 144 | + expect.objectContaining({ |
| 145 | + method: "PATCH", |
| 146 | + body: JSON.stringify({ enabled: false }), |
| 147 | + }), |
| 148 | + ); |
| 149 | + expect(fetch).toHaveBeenNthCalledWith( |
| 150 | + 3, |
| 151 | + new URL( |
| 152 | + "https://app.posthog.test/api/projects/42/task_automations/automation-1/run/", |
| 153 | + ), |
| 154 | + expect.objectContaining({ method: "POST" }), |
| 155 | + ); |
| 156 | + expect(fetch.mock.calls[2]?.[1]?.body).toBeUndefined(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("preserves validation detail, code, and field attribution", async () => { |
| 160 | + fetch.mockResolvedValueOnce( |
| 161 | + new Response( |
| 162 | + JSON.stringify({ |
| 163 | + type: "validation_error", |
| 164 | + code: "invalid_input", |
| 165 | + detail: "Enter a valid cron expression.", |
| 166 | + attr: "cron_expression", |
| 167 | + }), |
| 168 | + { |
| 169 | + status: 400, |
| 170 | + statusText: "Bad Request", |
| 171 | + headers: { "Content-Type": "application/json" }, |
| 172 | + }, |
| 173 | + ), |
| 174 | + ); |
| 175 | + |
| 176 | + const request = client.createTaskAutomation({ |
| 177 | + name: "Daily PRs", |
| 178 | + prompt: "Check PRs", |
| 179 | + repository: "posthog/posthog", |
| 180 | + cron_expression: "not a cron", |
| 181 | + timezone: "Europe/London", |
| 182 | + }); |
| 183 | + |
| 184 | + await expect(request).rejects.toBeInstanceOf(TaskAutomationValidationError); |
| 185 | + await expect(request).rejects.toMatchObject({ |
| 186 | + status: 400, |
| 187 | + code: "invalid_input", |
| 188 | + attr: "cron_expression", |
| 189 | + message: "Enter a valid cron expression.", |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments