-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmollifierSyntheticTrace.test.ts
More file actions
150 lines (136 loc) · 5.67 KB
/
mollifierSyntheticTrace.test.ts
File metadata and controls
150 lines (136 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { describe, expect, it, vi } from "vitest";
vi.mock("~/db.server", () => ({ prisma: {}, $replica: {} }));
import { buildSyntheticTraceForBufferedRun } from "~/v3/mollifier/syntheticTrace.server";
import type { SyntheticRun } from "~/v3/mollifier/readFallback.server";
const NOW = new Date("2026-05-22T10:00:00Z");
const ONE_MS_IN_NS = 1_000_000;
function makeSyntheticRun(overrides: Partial<SyntheticRun> = {}): SyntheticRun {
return {
id: "run_internal_1",
friendlyId: "run_friendly_1",
status: "QUEUED",
cancelledAt: undefined,
cancelReason: undefined,
delayUntil: undefined,
taskIdentifier: "hello-world",
createdAt: NOW,
payload: undefined,
payloadType: undefined,
metadata: undefined,
metadataType: undefined,
seedMetadata: undefined,
seedMetadataType: undefined,
idempotencyKey: undefined,
idempotencyKeyOptions: undefined,
isTest: false,
depth: 0,
ttl: undefined,
tags: [],
runTags: [],
lockedToVersion: undefined,
resumeParentOnCompletion: false,
parentTaskRunId: undefined,
traceId: "trace_1",
spanId: "span_1",
parentSpanId: undefined,
runtimeEnvironmentId: "env_a",
engine: "V2",
workerQueue: undefined,
queue: undefined,
concurrencyKey: undefined,
machinePreset: undefined,
realtimeStreamsVersion: undefined,
maxAttempts: undefined,
maxDurationInSeconds: undefined,
replayedFromTaskRunFriendlyId: undefined,
annotations: undefined,
traceContext: undefined,
scheduleId: undefined,
batchId: undefined,
parentTaskRunFriendlyId: undefined,
rootTaskRunFriendlyId: undefined,
...overrides,
};
}
describe("buildSyntheticTraceForBufferedRun", () => {
it("populates the synthesised root span from snapshot identity fields", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun());
expect(trace.events).toHaveLength(1);
const root = trace.events[0];
expect(root.id).toBe("span_1");
expect(root.data.message).toBe("hello-world");
expect(root.data.startTime).toEqual(NOW);
expect(root.data.isRoot).toBe(true);
expect(root.data.offset).toBe(0);
expect(root.data.level).toBe("TRACE");
});
it("defaults the span message to 'Task' when the snapshot has no taskIdentifier", () => {
const trace = buildSyntheticTraceForBufferedRun(
makeSyntheticRun({ taskIdentifier: undefined })
);
expect(trace.events[0].data.message).toBe("Task");
});
it("falls back to an empty-string span id when the snapshot has no spanId", () => {
const trace = buildSyntheticTraceForBufferedRun(
makeSyntheticRun({ spanId: undefined })
);
expect(trace.events[0].id).toBe("");
// Empty id still marks as root (it matches the rootId fallback).
expect(trace.events[0].data.isRoot).toBe(true);
});
it("renders a QUEUED buffered run as an executing, partial root span", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun({ status: "QUEUED" }));
expect(trace.rootSpanStatus).toBe("executing");
expect(trace.events[0].data.isPartial).toBe(true);
expect(trace.events[0].data.isError).toBe(false);
expect(trace.events[0].data.isCancelled).toBe(false);
// A partial span exposes duration as null (the timeline reads it as
// "still running"); see syntheticTrace.server.ts duration mapping.
expect(trace.events[0].data.duration).toBeNull();
});
it("renders a CANCELED buffered run as a completed, non-partial cancelled span", () => {
const trace = buildSyntheticTraceForBufferedRun(
makeSyntheticRun({ status: "CANCELED", cancelledAt: NOW })
);
expect(trace.rootSpanStatus).toBe("completed");
expect(trace.events[0].data.isPartial).toBe(false);
expect(trace.events[0].data.isCancelled).toBe(true);
expect(trace.events[0].data.isError).toBe(false);
// Non-partial: duration is the span's numeric value (0 here), not null.
expect(trace.events[0].data.duration).toBe(0);
});
it("renders a FAILED buffered run as a failed, non-partial errored span", () => {
const trace = buildSyntheticTraceForBufferedRun(
makeSyntheticRun({
status: "FAILED",
error: { code: "GATE_REJECTED", message: "buffer rejected the run" },
})
);
expect(trace.rootSpanStatus).toBe("failed");
expect(trace.events[0].data.isPartial).toBe(false);
expect(trace.events[0].data.isError).toBe(true);
expect(trace.events[0].data.isCancelled).toBe(false);
expect(trace.events[0].data.duration).toBe(0);
});
it("floors the trace duration to a minimum of 1ms (in nanoseconds) so the timeline has a positive extent", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun());
expect(trace.duration).toBe(ONE_MS_IN_NS);
});
it("reports the buffered createdAt as the trace's rootStartedAt and leaves startedAt null", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun());
expect(trace.rootStartedAt).toEqual(NOW);
expect(trace.startedAt).toBeNull();
});
it("returns no link or override metadata (buffered traces are single-span)", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun());
expect(trace.linkedRunIdBySpanId).toEqual({});
expect(trace.overridesBySpanId).toBeUndefined();
expect(trace.queuedDuration).toBeUndefined();
});
it("synthesises an empty timeline and keeps raw span events out of the payload", () => {
const trace = buildSyntheticTraceForBufferedRun(makeSyntheticRun());
// Raw span events stay server-side (mirrors RunPresenter's payload diet).
expect(trace.events[0].data).not.toHaveProperty("events");
expect(trace.events[0].data.timelineEvents).toEqual([]);
});
});