|
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" |
| 2 | +import { Telemetry } from "../../../src/altimate/telemetry" |
| 3 | +import { isWorkspaceClient, trackWorkspaceToolCall } from "../../../src/altimate/workspace/tool-call-telemetry" |
| 4 | + |
| 5 | +describe("workspace integration tool calls", () => { |
| 6 | + let rows: any[] |
| 7 | + let spy: ReturnType<typeof spyOn> |
| 8 | + beforeEach(() => { |
| 9 | + rows = [] |
| 10 | + spy = spyOn(Telemetry, "track").mockImplementation((e) => { |
| 11 | + rows.push(e) |
| 12 | + }) |
| 13 | + Telemetry.clearWorkspaceUse("s1") |
| 14 | + }) |
| 15 | + afterEach(() => { |
| 16 | + spy.mockRestore() |
| 17 | + Telemetry.clearWorkspaceUse("s1") |
| 18 | + }) |
| 19 | + |
| 20 | + const call = (clientName: string, outcome: "success" | "error", error?: unknown) => |
| 21 | + trackWorkspaceToolCall({ |
| 22 | + key: `${clientName}_jira_get_issue`, |
| 23 | + clientName, |
| 24 | + sessionID: "s1", |
| 25 | + messageID: "m1", |
| 26 | + startedAt: Date.now() - 5, |
| 27 | + outcome, |
| 28 | + error, |
| 29 | + }) |
| 30 | + |
| 31 | + test("the datamate client's calls become tool_call rows with via_workspace; a success counts as an integration use", () => { |
| 32 | + expect(call("datamate", "success")).toBe(true) |
| 33 | + expect(rows).toHaveLength(1) |
| 34 | + expect(rows[0]).toEqual( |
| 35 | + expect.objectContaining({ |
| 36 | + type: "tool_call", |
| 37 | + tool_name: "datamate_jira_get_issue", |
| 38 | + tool_type: "mcp", |
| 39 | + status: "success", |
| 40 | + via_workspace: true, |
| 41 | + session_id: "s1", |
| 42 | + message_id: "m1", |
| 43 | + }), |
| 44 | + ) |
| 45 | + expect(rows[0].duration_ms).toBeGreaterThanOrEqual(0) |
| 46 | + expect("error" in rows[0]).toBe(false) |
| 47 | + expect(Telemetry.workspaceUseOf("s1").integrations).toBe(1) |
| 48 | + }) |
| 49 | + |
| 50 | + test("a failed call is a tool_call error row with a masked message and no integration use", () => { |
| 51 | + expect(call("datamates", "error", new Error("boom at /home/someone/secret.txt"))).toBe(true) |
| 52 | + expect(rows[0]).toEqual(expect.objectContaining({ status: "error", via_workspace: true, tool_type: "mcp" })) |
| 53 | + expect(rows[0].error).not.toContain("/home/someone") |
| 54 | + expect(Telemetry.workspaceUseOf("s1").integrations).toBe(0) |
| 55 | + expect(call("datamate-acme", "error", "string failure")).toBe(true) |
| 56 | + expect(rows[1].error).toContain("string failure") |
| 57 | + }) |
| 58 | + |
| 59 | + test("other MCP clients are not tracked here, and a registry tool id is not a client", () => { |
| 60 | + expect(call("github", "success")).toBe(false) |
| 61 | + expect(call("filesystem", "error", new Error("x"))).toBe(false) |
| 62 | + expect(rows).toHaveLength(0) |
| 63 | + expect(Telemetry.workspaceUseOf("s1").integrations).toBe(0) |
| 64 | + // The registry tool `datamate_manager` runs through Tool.define, never through this path, and its |
| 65 | + // id is not a datamate client name either. |
| 66 | + expect(isWorkspaceClient("datamate_manager")).toBe(false) |
| 67 | + expect(isWorkspaceClient("datamate")).toBe(true) |
| 68 | + expect(isWorkspaceClient("Datamates")).toBe(true) |
| 69 | + expect(isWorkspaceClient("datamate-acme")).toBe(true) |
| 70 | + }) |
| 71 | +}) |
0 commit comments