|
1 | 1 | import { describe, expect, it, vi } from 'vitest' |
2 | 2 | import type { DAG, DAGNode } from '@/executor/dag/builder' |
3 | 3 | import { EdgeManager } from '@/executor/execution/edge-manager' |
4 | | -import { serializePauseSnapshot } from '@/executor/execution/snapshot-serializer' |
| 4 | +import { |
| 5 | + buildCompletedExecutionState, |
| 6 | + isLiveExecutionState, |
| 7 | + serializePauseSnapshot, |
| 8 | +} from '@/executor/execution/snapshot-serializer' |
5 | 9 | import type { ExecutionContext } from '@/executor/types' |
6 | 10 | import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' |
7 | 11 |
|
@@ -307,3 +311,82 @@ describe('serializePauseSnapshot', () => { |
307 | 311 | expect(serialized.metadata.capabilityGovernedUserId).toBe('requesting-member') |
308 | 312 | }) |
309 | 313 | }) |
| 314 | + |
| 315 | +describe('buildCompletedExecutionState', () => { |
| 316 | + /** The state a completed run used to carry: the pause snapshot's state, JSON round-tripped. */ |
| 317 | + function jsonClonedState(context: ExecutionContext): unknown { |
| 318 | + return JSON.parse(serializePauseSnapshot(context, []).snapshot).state |
| 319 | + } |
| 320 | + |
| 321 | + function contextWithOutput(output: unknown): ExecutionContext { |
| 322 | + return createContext({ |
| 323 | + blockStates: new Map([['block-1', { output, executed: true, executionTime: 5 }]]), |
| 324 | + executedBlocks: new Set(['block-1']), |
| 325 | + blockLogs: [ |
| 326 | + { |
| 327 | + blockId: 'block-1', |
| 328 | + blockName: 'Block', |
| 329 | + blockType: 'function', |
| 330 | + startedAt: '2026-01-01T00:00:00.000Z', |
| 331 | + endedAt: '2026-01-01T00:00:01.000Z', |
| 332 | + durationMs: 1000, |
| 333 | + success: true, |
| 334 | + executionOrder: 1, |
| 335 | + output, |
| 336 | + input: { note: undefined, list: [undefined, 1] }, |
| 337 | + }, |
| 338 | + ] as ExecutionContext['blockLogs'], |
| 339 | + }) |
| 340 | + } |
| 341 | + |
| 342 | + const shared = { rows: [{ id: 1, at: new Date(0) }] } |
| 343 | + const cyclic: Record<string, unknown> = { a: 1 } |
| 344 | + cyclic.self = cyclic |
| 345 | + const cycleBehindToJSON: Record<string, unknown> = { toJSON: () => ({ safe: true }) } |
| 346 | + cycleBehindToJSON.self = cycleBehindToJSON |
| 347 | + |
| 348 | + it.each([ |
| 349 | + ['a subtree shared twice', { first: shared, second: shared }], |
| 350 | + ['a cycle hidden behind toJSON', { value: cycleBehindToJSON }], |
| 351 | + [ |
| 352 | + 'a boxed number carrying a BigInt property', |
| 353 | + { value: Object.assign(Object(1), { big: BigInt(1) }) }, |
| 354 | + ], |
| 355 | + ])('serializes like the JSON-cloned pause state for %s', (_name, output) => { |
| 356 | + const context = contextWithOutput(output) |
| 357 | + expect(JSON.stringify(buildCompletedExecutionState(context))).toBe( |
| 358 | + JSON.stringify(jsonClonedState(context)) |
| 359 | + ) |
| 360 | + }) |
| 361 | + |
| 362 | + it.each([ |
| 363 | + ['a cycle', cyclic], |
| 364 | + ['a BigInt', { big: BigInt(1) }], |
| 365 | + ])('throws like the pause snapshot for %s', (_name, output) => { |
| 366 | + const context = contextWithOutput(output) |
| 367 | + expect(() => jsonClonedState(context)).toThrow(TypeError) |
| 368 | + expect(() => buildCompletedExecutionState(context)).toThrow(TypeError) |
| 369 | + }) |
| 370 | + |
| 371 | + it('shares block outputs with the run instead of cloning them', () => { |
| 372 | + const output = { rows: [{ id: 1 }] } |
| 373 | + const context = contextWithOutput(output) |
| 374 | + const state = buildCompletedExecutionState(context) |
| 375 | + |
| 376 | + expect(state.blockLogs[0].output).toBe(output) |
| 377 | + expect(state.blockStates['block-1'].output).toBe(output) |
| 378 | + |
| 379 | + context.blockLogs[0].endedAt = 'later' |
| 380 | + expect(state.blockLogs[0].endedAt).toBe('2026-01-01T00:00:01.000Z') |
| 381 | + }) |
| 382 | + |
| 383 | + it('marks completed state as live through spreads but not through JSON', () => { |
| 384 | + const context = contextWithOutput({ rows: [{ id: 1 }] }) |
| 385 | + const state = buildCompletedExecutionState(context) |
| 386 | + |
| 387 | + expect(isLiveExecutionState(state)).toBe(true) |
| 388 | + expect(isLiveExecutionState({ ...state, sourceExecutionId: 'other' })).toBe(true) |
| 389 | + expect(isLiveExecutionState(jsonClonedState(context))).toBe(false) |
| 390 | + expect(JSON.stringify(state)).toBe(JSON.stringify(jsonClonedState(context))) |
| 391 | + }) |
| 392 | +}) |
0 commit comments