|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { unregisterGlobal } from "../utils/globals.js"; |
| 3 | +import { SemanticInternalAttributes } from "../semanticInternalAttributes.js"; |
| 4 | +import { TaskContextAPI } from "./index.js"; |
| 5 | + |
| 6 | +const FAKE_CTX = { |
| 7 | + attempt: { id: "attempt_1", number: 1, startedAt: new Date(), status: "EXECUTING" as const }, |
| 8 | + run: { |
| 9 | + id: "run_1", |
| 10 | + payload: undefined, |
| 11 | + payloadType: "application/json", |
| 12 | + context: undefined, |
| 13 | + createdAt: new Date(), |
| 14 | + tags: [], |
| 15 | + isTest: false, |
| 16 | + isReplay: false, |
| 17 | + startedAt: new Date(), |
| 18 | + durationMs: 0, |
| 19 | + costInCents: 0, |
| 20 | + baseCostInCents: 0, |
| 21 | + }, |
| 22 | + task: { id: "my-task", filePath: "src/trigger/task.ts", exportName: "myTask" }, |
| 23 | + queue: { id: "queue_1", name: "default" }, |
| 24 | + environment: { id: "env_1", slug: "dev", type: "DEVELOPMENT" as const }, |
| 25 | + organization: { id: "org_1", slug: "acme", name: "Acme" }, |
| 26 | + project: { id: "proj_1", ref: "proj_xyz", slug: "demo", name: "Demo" }, |
| 27 | + machine: { |
| 28 | + name: "small-1x" as const, |
| 29 | + cpu: 0.5, |
| 30 | + memory: 0.5, |
| 31 | + centsPerMs: 0.0001, |
| 32 | + }, |
| 33 | +} as never; |
| 34 | + |
| 35 | +const FAKE_WORKER = { id: "worker_1", version: "1.0.0", contentHash: "abc" } as never; |
| 36 | + |
| 37 | +describe("TaskContextAPI conversation id", () => { |
| 38 | + afterEach(() => { |
| 39 | + unregisterGlobal("task-context"); |
| 40 | + TaskContextAPI.getInstance().setConversationId(undefined); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns no conversation attribute when setConversationId was never called", () => { |
| 44 | + const api = TaskContextAPI.getInstance(); |
| 45 | + api.setGlobalTaskContext({ ctx: FAKE_CTX, worker: FAKE_WORKER }); |
| 46 | + |
| 47 | + expect(api.attributes[SemanticInternalAttributes.GEN_AI_CONVERSATION_ID]).toBeUndefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("includes gen_ai.conversation.id after setConversationId", () => { |
| 51 | + const api = TaskContextAPI.getInstance(); |
| 52 | + api.setGlobalTaskContext({ ctx: FAKE_CTX, worker: FAKE_WORKER }); |
| 53 | + |
| 54 | + api.setConversationId("chat_123"); |
| 55 | + |
| 56 | + expect(api.attributes[SemanticInternalAttributes.GEN_AI_CONVERSATION_ID]).toBe("chat_123"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("clears the conversation attribute when called with undefined", () => { |
| 60 | + const api = TaskContextAPI.getInstance(); |
| 61 | + api.setGlobalTaskContext({ ctx: FAKE_CTX, worker: FAKE_WORKER }); |
| 62 | + api.setConversationId("chat_123"); |
| 63 | + |
| 64 | + api.setConversationId(undefined); |
| 65 | + |
| 66 | + expect(api.attributes[SemanticInternalAttributes.GEN_AI_CONVERSATION_ID]).toBeUndefined(); |
| 67 | + expect(api.conversationId).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns no attributes when there is no task context", () => { |
| 71 | + const api = TaskContextAPI.getInstance(); |
| 72 | + api.setConversationId("chat_123"); |
| 73 | + |
| 74 | + expect(api.attributes).toEqual({}); |
| 75 | + }); |
| 76 | + |
| 77 | + it("clears conversation id when a new task context is registered (warm restart)", () => { |
| 78 | + const api = TaskContextAPI.getInstance(); |
| 79 | + api.setGlobalTaskContext({ ctx: FAKE_CTX, worker: FAKE_WORKER }); |
| 80 | + api.setConversationId("chat_old"); |
| 81 | + |
| 82 | + api.setGlobalTaskContext({ ctx: FAKE_CTX, worker: FAKE_WORKER }); |
| 83 | + |
| 84 | + expect(api.attributes[SemanticInternalAttributes.GEN_AI_CONVERSATION_ID]).toBeUndefined(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments