|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { CommandInstrumentation } from "@/instrumentation/commands"; |
| 4 | + |
| 5 | +import { agent, resource, workspace } from "@repo/mocks"; |
| 6 | + |
| 7 | +import { createTelemetryHarness } from "../../mocks/telemetry"; |
| 8 | + |
| 9 | +function workspaceWithAgents() { |
| 10 | + const connected = agent({ |
| 11 | + status: "connected", |
| 12 | + lifecycle_state: "ready", |
| 13 | + }); |
| 14 | + const disconnected = agent({ |
| 15 | + id: "agent-2", |
| 16 | + name: "secondary", |
| 17 | + status: "disconnected", |
| 18 | + lifecycle_state: "off", |
| 19 | + }); |
| 20 | + return { |
| 21 | + connected, |
| 22 | + disconnected, |
| 23 | + workspace: workspace({ |
| 24 | + outdated: true, |
| 25 | + latest_build: { |
| 26 | + status: "running", |
| 27 | + resources: [resource({ agents: [connected, disconnected] })], |
| 28 | + }, |
| 29 | + }), |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("command instrumentation helpers", () => { |
| 34 | + it("records workspace selection without workspace or agent names", async () => { |
| 35 | + const { sink, service } = createTelemetryHarness(); |
| 36 | + const traces = new CommandInstrumentation(service); |
| 37 | + const selection = workspaceWithAgents(); |
| 38 | + |
| 39 | + await traces.workspaceOpen( |
| 40 | + "command", |
| 41 | + { workspace: selection.workspace, agent: selection.connected }, |
| 42 | + () => Promise.resolve(true), |
| 43 | + ); |
| 44 | + |
| 45 | + const event = sink.expectOne("workspace.open"); |
| 46 | + expect(event.properties).toMatchObject({ |
| 47 | + "agent.lifecycle_state": "ready", |
| 48 | + "agent.status": "connected", |
| 49 | + "workspace.outdated": "true", |
| 50 | + "workspace.status": "running", |
| 51 | + result: "success", |
| 52 | + }); |
| 53 | + expect(event.measurements).toMatchObject({ |
| 54 | + agentCount: 2, |
| 55 | + connectedAgentCount: 1, |
| 56 | + }); |
| 57 | + expect(event.properties.workspaceName).toBeUndefined(); |
| 58 | + expect(event.properties.agentName).toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("records workspace picker cancellation and failure distinctly", async () => { |
| 62 | + const { sink, service } = createTelemetryHarness(); |
| 63 | + const traces = new CommandInstrumentation(service); |
| 64 | + |
| 65 | + await traces.workspacePicker("workspace.open", (telemetry) => { |
| 66 | + telemetry.cancelled(3); |
| 67 | + return Promise.resolve({ status: "cancelled" }); |
| 68 | + }); |
| 69 | + await traces.workspacePicker("workspace.open", (telemetry) => { |
| 70 | + telemetry.failed("fetch_failed", 0); |
| 71 | + return Promise.resolve({ status: "failed", category: "fetch_failed" }); |
| 72 | + }); |
| 73 | + |
| 74 | + const [cancelled, failed] = sink.eventsNamed("workspace.picker.prompted"); |
| 75 | + expect(cancelled.properties).toMatchObject({ result: "aborted" }); |
| 76 | + expect(cancelled.measurements.workspaceCount).toBe(3); |
| 77 | + expect(failed.properties).toMatchObject({ |
| 78 | + "failure.category": "fetch_failed", |
| 79 | + result: "error", |
| 80 | + }); |
| 81 | + expect(failed.measurements.workspaceCount).toBe(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it("records workspace open cancellation and handled failure distinctly", async () => { |
| 85 | + const { sink, service } = createTelemetryHarness(); |
| 86 | + const traces = new CommandInstrumentation(service); |
| 87 | + const selection = workspaceWithAgents(); |
| 88 | + |
| 89 | + await traces.workspaceOpen("command", undefined, (telemetry) => |
| 90 | + Promise.resolve( |
| 91 | + telemetry.cancel("agent_picker", { workspace: selection.workspace }), |
| 92 | + ), |
| 93 | + ); |
| 94 | + await traces.workspaceOpen("command", undefined, (telemetry) => |
| 95 | + Promise.resolve(telemetry.fail("fetch_failed")), |
| 96 | + ); |
| 97 | + |
| 98 | + const [cancelled, failed] = sink.eventsNamed("workspace.open"); |
| 99 | + expect(cancelled.properties).toMatchObject({ |
| 100 | + "cancel.stage": "agent_picker", |
| 101 | + "workspace.status": "running", |
| 102 | + result: "aborted", |
| 103 | + }); |
| 104 | + expect(failed.properties).toMatchObject({ |
| 105 | + "failure.category": "fetch_failed", |
| 106 | + result: "error", |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("records thrown workspace open failures without raw error details", async () => { |
| 111 | + const { sink, service } = createTelemetryHarness(); |
| 112 | + const traces = new CommandInstrumentation(service); |
| 113 | + |
| 114 | + await expect( |
| 115 | + traces.workspaceOpen("command", undefined, () => |
| 116 | + Promise.reject(new Error("secret path /tmp/workspace")), |
| 117 | + ), |
| 118 | + ).rejects.toThrow("secret path /tmp/workspace"); |
| 119 | + |
| 120 | + const event = sink.expectOne("workspace.open"); |
| 121 | + expect(event.properties).toMatchObject({ |
| 122 | + "failure.category": "error", |
| 123 | + result: "error", |
| 124 | + }); |
| 125 | + expect(event.error).toBeUndefined(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("records diagnostic cancellation and failure categories", async () => { |
| 129 | + const { sink, service } = createTelemetryHarness(); |
| 130 | + const traces = new CommandInstrumentation(service); |
| 131 | + const failure = new Error("boom"); |
| 132 | + |
| 133 | + await traces.diagnostic("coder.supportBundle", (telemetry) => { |
| 134 | + telemetry.cancel("save_dialog"); |
| 135 | + return Promise.resolve(); |
| 136 | + }); |
| 137 | + await traces.diagnostic("coder.supportBundle", (telemetry) => { |
| 138 | + telemetry.fail(failure, "unsupported_cli"); |
| 139 | + return Promise.resolve(); |
| 140 | + }); |
| 141 | + |
| 142 | + const [cancelled, failed] = sink.eventsNamed( |
| 143 | + "command.diagnostic.completed", |
| 144 | + ); |
| 145 | + expect(cancelled.properties).toMatchObject({ |
| 146 | + "cancel.stage": "save_dialog", |
| 147 | + result: "aborted", |
| 148 | + }); |
| 149 | + expect(failed.properties).toMatchObject({ |
| 150 | + "failure.category": "unsupported_cli", |
| 151 | + result: "error", |
| 152 | + }); |
| 153 | + expect(failed.error).toBeUndefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("records bounded speed test measurements", async () => { |
| 157 | + const { sink, service } = createTelemetryHarness(); |
| 158 | + const traces = new CommandInstrumentation(service); |
| 159 | + |
| 160 | + await traces.diagnostic("coder.speedTest", (telemetry) => { |
| 161 | + const parsed = telemetry.speedtestSuccess( |
| 162 | + JSON.stringify({ |
| 163 | + overall: { |
| 164 | + start_time_seconds: 0, |
| 165 | + end_time_seconds: 5, |
| 166 | + throughput_mbits: 42, |
| 167 | + }, |
| 168 | + intervals: [ |
| 169 | + { |
| 170 | + start_time_seconds: 0, |
| 171 | + end_time_seconds: 5, |
| 172 | + throughput_mbits: 42, |
| 173 | + }, |
| 174 | + ], |
| 175 | + }), |
| 176 | + ); |
| 177 | + expect(parsed.overall.throughput_mbits).toBe(42); |
| 178 | + return Promise.resolve(); |
| 179 | + }); |
| 180 | + |
| 181 | + expect(sink.expectOne("command.diagnostic.completed")).toMatchObject({ |
| 182 | + measurements: { |
| 183 | + intervalCount: 1, |
| 184 | + throughputMbits: 42, |
| 185 | + }, |
| 186 | + properties: { result: "success" }, |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it("records thrown devcontainer failures without raw error details", async () => { |
| 191 | + const { sink, service } = createTelemetryHarness(); |
| 192 | + const traces = new CommandInstrumentation(service); |
| 193 | + |
| 194 | + await expect( |
| 195 | + traces.devcontainerOpen("dev_container", () => |
| 196 | + Promise.reject(new Error("secret local path /tmp/workspace")), |
| 197 | + ), |
| 198 | + ).rejects.toThrow("secret local path /tmp/workspace"); |
| 199 | + |
| 200 | + const event = sink.expectOne("workspace.devcontainer.open"); |
| 201 | + expect(event.properties).toMatchObject({ |
| 202 | + "failure.category": "error", |
| 203 | + mode: "dev_container", |
| 204 | + result: "error", |
| 205 | + }); |
| 206 | + expect(event.error).toBeUndefined(); |
| 207 | + }); |
| 208 | +}); |
0 commit comments