|
| 1 | +// altimate_change - new file |
| 2 | +// |
| 3 | +// `starter_action_picked`: one row per selection from a TUI starter dialog, naming the action, the |
| 4 | +// prompt's position for a prompt, and the starter's own workspace; never the prompt's text. |
| 5 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" |
| 6 | +import * as Contents from "../../../src/altimate/workspace/contents" |
| 7 | +import type { WorkspaceContents } from "../../../src/altimate/workspace/contents" |
| 8 | +import { starterFor } from "../../../src/altimate/workspace/starter" |
| 9 | +import { |
| 10 | + starterActionPickedEvent, |
| 11 | + starterPick, |
| 12 | + trackStarterActionPicked, |
| 13 | +} from "../../../src/altimate/workspace/starter-telemetry" |
| 14 | +import { Telemetry } from "../../../src/altimate/telemetry" |
| 15 | + |
| 16 | +const contents: WorkspaceContents = { |
| 17 | + skills: [{ name: "incident-runbook", description: "" }], |
| 18 | + integrations: [], |
| 19 | + memoryEnabled: true, |
| 20 | + knowledge: { kind: "off" }, |
| 21 | +} |
| 22 | + |
| 23 | +let events: any[] |
| 24 | +let trackSpy: ReturnType<typeof spyOn> |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + events = [] |
| 28 | + trackSpy = spyOn(Telemetry, "track").mockImplementation((e) => { |
| 29 | + events.push(e) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +afterEach(() => { |
| 34 | + trackSpy.mockRestore() |
| 35 | +}) |
| 36 | + |
| 37 | +describe("starterPick", () => { |
| 38 | + test.each([ |
| 39 | + ["prompt:0", { action: "prompt", promptIndex: 0 }], |
| 40 | + ["prompt:2", { action: "prompt", promptIndex: 2 }], |
| 41 | + ["open", { action: "open" }], |
| 42 | + ["done", { action: "done" }], |
| 43 | + ] as const)("%s is a starter action", (value, pick) => { |
| 44 | + expect(starterPick(value)).toEqual(pick) |
| 45 | + }) |
| 46 | + |
| 47 | + test.each(["", "prompt:", "prompt:-1", "prompt:1.5", "prompt:x", "status", "Done", " done"])( |
| 48 | + "%p is not a starter action", |
| 49 | + (value) => { |
| 50 | + expect(starterPick(value)).toBeNull() |
| 51 | + }, |
| 52 | + ) |
| 53 | +}) |
| 54 | + |
| 55 | +describe("starter_action_picked row", () => { |
| 56 | + test("a prompt carries its index and the workspace, and no text", () => { |
| 57 | + const row = starterActionPickedEvent(7, { action: "prompt", promptIndex: 1 }, "ses_1") as Record<string, unknown> |
| 58 | + expect(Object.keys(row).sort()).toEqual( |
| 59 | + ["action", "prompt_index", "session_id", "timestamp", "type", "workspace_id"].sort(), |
| 60 | + ) |
| 61 | + expect(row).toEqual( |
| 62 | + expect.objectContaining({ |
| 63 | + type: "starter_action_picked", |
| 64 | + session_id: "ses_1", |
| 65 | + workspace_id: 7, |
| 66 | + action: "prompt", |
| 67 | + prompt_index: 1, |
| 68 | + }), |
| 69 | + ) |
| 70 | + }) |
| 71 | + |
| 72 | + test.each(["open", "done"] as const)("%s carries no prompt_index", (action) => { |
| 73 | + const row = starterActionPickedEvent(7, { action }, "ses_1") as Record<string, unknown> |
| 74 | + expect(row.action).toBe(action) |
| 75 | + expect("prompt_index" in row).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + test("falls back to the process context's session", () => { |
| 79 | + const row = starterActionPickedEvent(7, { action: "done" }) |
| 80 | + expect(row.session_id).toBe(Telemetry.getContext().sessionId) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +describe("trackStarterActionPicked", () => { |
| 85 | + test("tracks exactly one row per selection, against the starter's workspace", () => { |
| 86 | + expect(trackStarterActionPicked({ workspaceId: 9 }, "prompt:2")).toBe(true) |
| 87 | + expect(events).toHaveLength(1) |
| 88 | + expect(events[0]).toEqual( |
| 89 | + expect.objectContaining({ type: "starter_action_picked", workspace_id: 9, action: "prompt", prompt_index: 2 }), |
| 90 | + ) |
| 91 | + expect(JSON.stringify(events[0])).not.toContain("Which") |
| 92 | + }) |
| 93 | + |
| 94 | + test("nothing before the starter arrives: the dialog is then the bare link confirmation", () => { |
| 95 | + expect(trackStarterActionPicked(null, "done")).toBe(false) |
| 96 | + expect(trackStarterActionPicked(null, "open")).toBe(false) |
| 97 | + expect(events).toEqual([]) |
| 98 | + }) |
| 99 | + |
| 100 | + test("nothing for a value that is not a starter action", () => { |
| 101 | + expect(trackStarterActionPicked({ workspaceId: 9 }, "status")).toBe(false) |
| 102 | + expect(events).toEqual([]) |
| 103 | + }) |
| 104 | + |
| 105 | + test.each([undefined, 0, -3, 1.5, Number.NaN])("nothing for workspace id %p", (workspaceId) => { |
| 106 | + expect(trackStarterActionPicked({ workspaceId }, "done")).toBe(false) |
| 107 | + expect(events).toEqual([]) |
| 108 | + }) |
| 109 | + |
| 110 | + test("never throws when telemetry does", () => { |
| 111 | + trackSpy.mockImplementation(() => { |
| 112 | + throw new Error("boom") |
| 113 | + }) |
| 114 | + expect(trackStarterActionPicked({ workspaceId: 9 }, "done")).toBe(false) |
| 115 | + }) |
| 116 | +}) |
| 117 | + |
| 118 | +describe("starterFor names its workspace", () => { |
| 119 | + test("the served starter carries the id its starter_shown row reported", async () => { |
| 120 | + const contentsSpy = spyOn(Contents, "workspaceContents").mockResolvedValue(contents) |
| 121 | + try { |
| 122 | + const starter = await starterFor("/tmp/project", { datamateId: 31, datamateName: "analytics" }) |
| 123 | + expect(starter.workspaceId).toBe(31) |
| 124 | + const shown = events.find((e) => e.type === "starter_shown") |
| 125 | + expect(shown?.workspace_id).toBe(starter.workspaceId) |
| 126 | + events.length = 0 |
| 127 | + trackStarterActionPicked(starter, "prompt:0") |
| 128 | + expect(events[0].workspace_id).toBe(shown.workspace_id) |
| 129 | + } finally { |
| 130 | + contentsSpy.mockRestore() |
| 131 | + } |
| 132 | + }) |
| 133 | +}) |
0 commit comments