|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { join, resolve } from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +/** |
| 7 | + * slack()'s `filter` and `startOn` are checked against the endpoint's event type, and an invalid one |
| 8 | + * shows the validator's `✖ ...` message. These compile a consumer file with tsc and assert the |
| 9 | + * diagnostic text at each call, since `@ts-expect-error` passes on any error. |
| 10 | + */ |
| 11 | + |
| 12 | +const packageRoot = resolve(__dirname, ".."); |
| 13 | +const repoRoot = resolve(packageRoot, "../.."); |
| 14 | + |
| 15 | +const HEADER = ` |
| 16 | +import { webhooks } from "@trigger.dev/sdk"; |
| 17 | +import { chat } from "@trigger.dev/sdk/ai"; |
| 18 | +import { mentions, slack, webhookSource, type SlackRunSurface } from "../src/index.js"; |
| 19 | +
|
| 20 | +const endpoint = webhooks.endpoint.define({ id: "slack-types", source: webhookSource() }); |
| 21 | +const token = "xoxb-test"; |
| 22 | +`; |
| 23 | + |
| 24 | +const CASES: Array<{ name: string; code: string; message?: string }> = [ |
| 25 | + { |
| 26 | + name: "filter with an unknown field", |
| 27 | + code: `slack({ startOn: "event.event.type == 'message'", id: "a", endpoint, token, filter: "event.event.nope == 'x'" });`, |
| 28 | + message: "✖ unknown field: event.event.nope", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "filter with unbalanced parens", |
| 32 | + code: `slack({ startOn: "event.event.type == 'message'", id: "b", endpoint, token, filter: "( event.event.channel == 'C1'" });`, |
| 33 | + message: '✖ unbalanced parens (missing \\")\\")', |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "startOn with an unknown field", |
| 37 | + code: `slack({ id: "c", endpoint, token, startOn: "event.event.txt contains 'deploy'" });`, |
| 38 | + message: "✖ unknown field: event.event.txt", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "valid filters compile", |
| 42 | + code: `slack({ id: "d", endpoint, token, filter: "event.event.channel == 'C1' && event.team_id == 'T1'", startOn: "event.event.text contains '<@U012BOT>'" });`, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "mentions() and filters built at runtime compile", |
| 46 | + code: `const channels: string = ["C1", "C2"].map((c) => \`event.event.channel == '\${c}'\`).join(" || "); slack({ id: "e", endpoint, token, filter: channels, startOn: mentions("U012BOT") });`, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "omitted filter compiles", |
| 50 | + code: `slack({ startOn: "event.event.type == 'message'", id: "f", endpoint, token });`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "chat.surface<SlackRunSurface>() types the Slack helpers", |
| 54 | + code: `void (async () => { const surface = chat.surface<SlackRunSurface>(); await surface?.post({ text: "hi" }); await surface?.react("eyes"); })();`, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "startOn is required", |
| 58 | + code: `slack({ id: "g", endpoint, token });`, |
| 59 | + message: "startOn", |
| 60 | + }, |
| 61 | +]; |
| 62 | + |
| 63 | +function diagnosticsByLine(): Map<number, string[]> { |
| 64 | + const dir = mkdtempSync(join(packageRoot, ".filter-types-")); |
| 65 | + try { |
| 66 | + writeFileSync(join(dir, "consumer.ts"), `${HEADER}\n${CASES.map((c) => c.code).join("\n")}\n`); |
| 67 | + writeFileSync( |
| 68 | + join(dir, "tsconfig.json"), |
| 69 | + JSON.stringify({ |
| 70 | + extends: join(packageRoot, "tsconfig.src.json"), |
| 71 | + compilerOptions: { noEmit: true, composite: false, incremental: false, rootDir: repoRoot }, |
| 72 | + include: ["consumer.ts"], |
| 73 | + exclude: [], |
| 74 | + }) |
| 75 | + ); |
| 76 | + const result = spawnSync( |
| 77 | + join(repoRoot, "node_modules/.bin/tsc"), |
| 78 | + ["-p", dir, "--pretty", "false"], |
| 79 | + { encoding: "utf8" } |
| 80 | + ); |
| 81 | + const byLine = new Map<number, string[]>(); |
| 82 | + let last: number | undefined; |
| 83 | + for (const line of `${result.stdout}${result.stderr}`.split("\n")) { |
| 84 | + const match = /^(.*?)\((\d+),\d+\): error TS\d+: (.*)$/.exec(line); |
| 85 | + if (match) { |
| 86 | + last = match[1]!.endsWith("consumer.ts") ? Number(match[2]) - 1 : -1; |
| 87 | + byLine.set(last, [...(byLine.get(last) ?? []), match[3]!]); |
| 88 | + } else if (last !== undefined && line.startsWith(" ")) { |
| 89 | + byLine.get(last)!.push(line.trim()); |
| 90 | + } |
| 91 | + } |
| 92 | + return byLine; |
| 93 | + } finally { |
| 94 | + rmSync(dir, { recursive: true, force: true }); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +describe("slack() filter types", () => { |
| 99 | + const headerLines = `${HEADER}\n`.split("\n").length - 1; |
| 100 | + let byLine: Map<number, string[]>; |
| 101 | + const at = (index: number) => byLine.get(headerLines + index) ?? []; |
| 102 | + |
| 103 | + it("compiles the consumer file", () => { |
| 104 | + byLine = diagnosticsByLine(); |
| 105 | + const outside = [...byLine.entries()].filter( |
| 106 | + ([line]) => line < headerLines || line >= headerLines + CASES.length |
| 107 | + ); |
| 108 | + expect(outside).toEqual([]); |
| 109 | + }, 120_000); |
| 110 | + |
| 111 | + for (const [index, testCase] of CASES.entries()) { |
| 112 | + it(testCase.name, () => { |
| 113 | + if (testCase.message) { |
| 114 | + expect(at(index).join("\n")).toContain(testCase.message); |
| 115 | + } else { |
| 116 | + expect(at(index)).toEqual([]); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | +}); |
0 commit comments