|
| 1 | +import { serve } from "bun"; |
| 2 | +import { describe, expect, it } from "bun:test"; |
| 3 | +import { |
| 4 | + createJSONResponse, |
| 5 | + execContainer, |
| 6 | + findResourceInstance, |
| 7 | + runContainer, |
| 8 | + runTerraformApply, |
| 9 | + runTerraformInit, |
| 10 | + testRequiredVariables, |
| 11 | +} from "../test"; |
| 12 | + |
| 13 | +describe("slackme", async () => { |
| 14 | + await runTerraformInit(import.meta.dir); |
| 15 | + |
| 16 | + testRequiredVariables(import.meta.dir, { |
| 17 | + agent_id: "foo", |
| 18 | + auth_provider_id: "foo", |
| 19 | + }); |
| 20 | + |
| 21 | + it("writes to path as executable", async () => { |
| 22 | + const { instance, id } = await setupContainer(); |
| 23 | + await writeCoder(id, "exit 0"); |
| 24 | + let exec = await execContainer(id, ["sh", "-c", instance.script]); |
| 25 | + expect(exec.exitCode).toBe(0); |
| 26 | + exec = await execContainer(id, ["sh", "-c", "which slackme"]); |
| 27 | + expect(exec.exitCode).toBe(0); |
| 28 | + expect(exec.stdout.trim()).toEqual("/usr/bin/slackme"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("prints usage with no command", async () => { |
| 32 | + const { instance, id } = await setupContainer(); |
| 33 | + await writeCoder(id, "echo 👋"); |
| 34 | + let exec = await execContainer(id, ["sh", "-c", instance.script]); |
| 35 | + expect(exec.exitCode).toBe(0); |
| 36 | + exec = await execContainer(id, ["sh", "-c", "slackme"]); |
| 37 | + expect(exec.stdout.trim()).toStartWith( |
| 38 | + "slackme — Send a Slack notification when a command finishes", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("displays url when not authenticated", async () => { |
| 43 | + const { instance, id } = await setupContainer(); |
| 44 | + await writeCoder(id, "echo 'some-url' && exit 1"); |
| 45 | + let exec = await execContainer(id, ["sh", "-c", instance.script]); |
| 46 | + expect(exec.exitCode).toBe(0); |
| 47 | + exec = await execContainer(id, ["sh", "-c", "slackme echo test"]); |
| 48 | + expect(exec.stdout.trim()).toEndWith("some-url"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("default output", async () => { |
| 52 | + await assertSlackMessage({ |
| 53 | + command: "echo test", |
| 54 | + durationMS: 2, |
| 55 | + output: "👨💻 `echo test` completed in 2ms", |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("formats multiline message", async () => { |
| 60 | + await assertSlackMessage({ |
| 61 | + command: "echo test", |
| 62 | + format: `this command: |
| 63 | +\`$COMMAND\` |
| 64 | +executed`, |
| 65 | + output: `this command: |
| 66 | +\`echo test\` |
| 67 | +executed`, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("formats execution with milliseconds", async () => { |
| 72 | + await assertSlackMessage({ |
| 73 | + command: "echo test", |
| 74 | + format: `$COMMAND took $DURATION`, |
| 75 | + durationMS: 150, |
| 76 | + output: "echo test took 150ms", |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("formats execution with seconds", async () => { |
| 81 | + await assertSlackMessage({ |
| 82 | + command: "echo test", |
| 83 | + format: `$COMMAND took $DURATION`, |
| 84 | + durationMS: 15000, |
| 85 | + output: "echo test took 15.0s", |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("formats execution with minutes", async () => { |
| 90 | + await assertSlackMessage({ |
| 91 | + command: "echo test", |
| 92 | + format: `$COMMAND took $DURATION`, |
| 93 | + durationMS: 120000, |
| 94 | + output: "echo test took 2m 0.0s", |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("formats execution with hours", async () => { |
| 99 | + await assertSlackMessage({ |
| 100 | + command: "echo test", |
| 101 | + format: `$COMMAND took $DURATION`, |
| 102 | + durationMS: 60000 * 60, |
| 103 | + output: "echo test took 1hr 0m 0.0s", |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +const setupContainer = async ( |
| 109 | + image = "alpine", |
| 110 | + vars: Record<string, string> = {}, |
| 111 | +) => { |
| 112 | + const state = await runTerraformApply(import.meta.dir, { |
| 113 | + agent_id: "foo", |
| 114 | + auth_provider_id: "foo", |
| 115 | + ...vars, |
| 116 | + }); |
| 117 | + const instance = findResourceInstance(state, "coder_script"); |
| 118 | + const id = await runContainer(image); |
| 119 | + return { id, instance }; |
| 120 | +}; |
| 121 | + |
| 122 | +const writeCoder = async (id: string, script: string) => { |
| 123 | + const exec = await execContainer(id, [ |
| 124 | + "sh", |
| 125 | + "-c", |
| 126 | + `echo '${script}' > /usr/bin/coder && chmod +x /usr/bin/coder`, |
| 127 | + ]); |
| 128 | + expect(exec.exitCode).toBe(0); |
| 129 | +}; |
| 130 | + |
| 131 | +const assertSlackMessage = async (opts: { |
| 132 | + command: string; |
| 133 | + format?: string; |
| 134 | + durationMS?: number; |
| 135 | + output: string; |
| 136 | +}) => { |
| 137 | + let url: URL; |
| 138 | + const fakeSlackHost = serve({ |
| 139 | + fetch: (req) => { |
| 140 | + url = new URL(req.url); |
| 141 | + if (url.pathname === "/api/chat.postMessage") |
| 142 | + return createJSONResponse({ |
| 143 | + ok: true, |
| 144 | + }); |
| 145 | + return createJSONResponse({}, 404); |
| 146 | + }, |
| 147 | + port: 0, |
| 148 | + }); |
| 149 | + const { instance, id } = await setupContainer( |
| 150 | + "alpine/curl", |
| 151 | + opts.format && { |
| 152 | + slack_message: opts.format, |
| 153 | + }, |
| 154 | + ); |
| 155 | + await writeCoder(id, "echo 'token'"); |
| 156 | + let exec = await execContainer(id, ["sh", "-c", instance.script]); |
| 157 | + expect(exec.exitCode).toBe(0); |
| 158 | + exec = await execContainer(id, [ |
| 159 | + "sh", |
| 160 | + "-c", |
| 161 | + `DURATION_MS=${opts.durationMS || 0} SLACK_URL="http://${ |
| 162 | + fakeSlackHost.hostname |
| 163 | + }:${fakeSlackHost.port}" slackme ${opts.command}`, |
| 164 | + ]); |
| 165 | + expect(exec.stderr.trim()).toBe(""); |
| 166 | + expect(url.pathname).toEqual("/api/chat.postMessage"); |
| 167 | + expect(url.searchParams.get("channel")).toEqual("token"); |
| 168 | + expect(url.searchParams.get("text")).toEqual(opts.output); |
| 169 | +}; |
0 commit comments