|
| 1 | +import type { Context } from "@temporalio/activity"; |
| 2 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 3 | +import { createBraintrustActivityInterceptor } from "./interceptors"; |
| 4 | +import { BRAINTRUST_SPAN_HEADER, serializeHeaderValue } from "./utils"; |
| 5 | + |
| 6 | +const mock = vi.hoisted(() => { |
| 7 | + const span = { log: vi.fn(), end: vi.fn() }; |
| 8 | + return { span, startSpan: vi.fn(() => span) }; |
| 9 | +}); |
| 10 | + |
| 11 | +vi.mock("braintrust", () => ({ |
| 12 | + startSpan: mock.startSpan, |
| 13 | + withCurrent: <R>(_span: unknown, callback: () => R): R => callback(), |
| 14 | +})); |
| 15 | + |
| 16 | +beforeEach(() => vi.clearAllMocks()); |
| 17 | + |
| 18 | +describe.each([ |
| 19 | + { |
| 20 | + name: "workflow", |
| 21 | + workflowExecution: { workflowId: "workflow", runId: "run" }, |
| 22 | + }, |
| 23 | + { name: "standalone", workflowExecution: undefined }, |
| 24 | +])("$name activity", ({ workflowExecution }) => { |
| 25 | + const context = { |
| 26 | + info: { |
| 27 | + activityId: "activity", |
| 28 | + activityType: "generate", |
| 29 | + workflowExecution, |
| 30 | + }, |
| 31 | + } as Context; |
| 32 | + |
| 33 | + test("runs the activity and preserves parent context and output", async () => { |
| 34 | + const interceptor = createBraintrustActivityInterceptor(context).inbound!; |
| 35 | + const input = { |
| 36 | + args: ["prompt"], |
| 37 | + headers: { [BRAINTRUST_SPAN_HEADER]: serializeHeaderValue("parent") }, |
| 38 | + }; |
| 39 | + const output = { text: "result" }; |
| 40 | + const next = vi.fn().mockResolvedValue(output); |
| 41 | + |
| 42 | + await expect(interceptor.execute!(input, next)).resolves.toBe(output); |
| 43 | + expect(next).toHaveBeenCalledExactlyOnceWith(input); |
| 44 | + expect(mock.startSpan).toHaveBeenCalledWith( |
| 45 | + expect.objectContaining({ |
| 46 | + parent: "parent", |
| 47 | + event: { |
| 48 | + metadata: { |
| 49 | + "temporal.activity_type": "generate", |
| 50 | + "temporal.activity_id": "activity", |
| 51 | + "temporal.workflow_id": workflowExecution?.workflowId, |
| 52 | + "temporal.workflow_run_id": workflowExecution?.runId, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }), |
| 56 | + ); |
| 57 | + expect(mock.span.log).toHaveBeenCalledWith({ output }); |
| 58 | + expect(mock.span.end).toHaveBeenCalledOnce(); |
| 59 | + }); |
| 60 | + |
| 61 | + test("preserves activity errors and ends the span", async () => { |
| 62 | + const interceptor = createBraintrustActivityInterceptor(context).inbound!; |
| 63 | + const error = new Error("activity failed"); |
| 64 | + const next = vi.fn().mockRejectedValue(error); |
| 65 | + |
| 66 | + await expect( |
| 67 | + interceptor.execute!({ args: [], headers: {} }, next), |
| 68 | + ).rejects.toBe(error); |
| 69 | + expect(next).toHaveBeenCalledOnce(); |
| 70 | + expect(mock.span.log).toHaveBeenCalledWith({ error: String(error) }); |
| 71 | + expect(mock.span.end).toHaveBeenCalledOnce(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments