|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 2 | + |
| 3 | +import { baselineStore } from '../src/baselineStore.js' |
| 4 | + |
| 5 | +const SUITE_UID = 'suite-1' |
| 6 | +const TEST_UID = 'test-1' |
| 7 | + |
| 8 | +function suite(opts: { |
| 9 | + start: number |
| 10 | + end?: number |
| 11 | + state?: 'passed' | 'failed' | 'pending' | 'running' |
| 12 | + childState?: 'passed' | 'failed' |
| 13 | + childError?: { message: string } |
| 14 | +}) { |
| 15 | + return [ |
| 16 | + { |
| 17 | + [SUITE_UID]: { |
| 18 | + uid: SUITE_UID, |
| 19 | + title: 'My Suite', |
| 20 | + file: '/spec.ts', |
| 21 | + start: opts.start, |
| 22 | + end: opts.end, |
| 23 | + state: opts.state, |
| 24 | + tests: [ |
| 25 | + { |
| 26 | + uid: TEST_UID, |
| 27 | + title: 'should do a thing', |
| 28 | + fullTitle: 'My Suite should do a thing', |
| 29 | + start: opts.start, |
| 30 | + end: opts.end, |
| 31 | + state: opts.childState, |
| 32 | + error: opts.childError |
| 33 | + } |
| 34 | + ], |
| 35 | + suites: [] |
| 36 | + } |
| 37 | + } |
| 38 | + ] |
| 39 | +} |
| 40 | + |
| 41 | +describe('baselineStore', () => { |
| 42 | + beforeEach(() => { |
| 43 | + baselineStore.resetActiveRun() |
| 44 | + baselineStore.clearAll() |
| 45 | + }) |
| 46 | + |
| 47 | + it('filters commands to the test time window and ignores commands outside it', () => { |
| 48 | + baselineStore.recordEvent('commands', [ |
| 49 | + { timestamp: 100, command: 'before', args: [] }, |
| 50 | + { timestamp: 250, command: 'inside', args: [] }, |
| 51 | + { timestamp: 900, command: 'after', args: [] } |
| 52 | + ]) |
| 53 | + baselineStore.recordEvent('suites', suite({ start: 200, end: 300 })) |
| 54 | + |
| 55 | + const snap = baselineStore.snapshot(TEST_UID, 'test')! |
| 56 | + expect(snap.commands.map((c) => c.command)).toEqual(['inside']) |
| 57 | + expect(baselineStore.snapshot('does-not-exist', 'test')).toBeUndefined() |
| 58 | + }) |
| 59 | + |
| 60 | + it('replaces (not unions) the time window when a new run is detected', () => { |
| 61 | + baselineStore.recordEvent( |
| 62 | + 'suites', |
| 63 | + suite({ |
| 64 | + start: 100, |
| 65 | + end: 200, |
| 66 | + state: 'failed', |
| 67 | + childState: 'failed', |
| 68 | + childError: { message: 'old failure' } |
| 69 | + }) |
| 70 | + ) |
| 71 | + baselineStore.recordEvent('commands', [ |
| 72 | + { timestamp: 150, command: 'first-run', args: [] } |
| 73 | + ]) |
| 74 | + // Incoming.start > previous.end → isNewRun |
| 75 | + baselineStore.recordEvent( |
| 76 | + 'suites', |
| 77 | + suite({ start: 500, end: 600, state: 'passed', childState: 'passed' }) |
| 78 | + ) |
| 79 | + baselineStore.recordEvent('commands', [ |
| 80 | + { timestamp: 550, command: 'second-run', args: [] } |
| 81 | + ]) |
| 82 | + |
| 83 | + const snap = baselineStore.snapshot(TEST_UID, 'test')! |
| 84 | + expect(snap.window).toEqual({ start: 500, end: 600 }) |
| 85 | + expect(snap.commands.map((c) => c.command)).toEqual(['second-run']) |
| 86 | + // State + error reset on the new run — no stale failure leak. |
| 87 | + expect(snap.test.state).toBe('passed') |
| 88 | + expect(snap.test.error).toBeUndefined() |
| 89 | + }) |
| 90 | + |
| 91 | + it("rolls a failing descendant's state + error up to a suite-scope snapshot", () => { |
| 92 | + baselineStore.recordEvent('commands', [ |
| 93 | + { timestamp: 150, command: 'click', args: [] } |
| 94 | + ]) |
| 95 | + baselineStore.recordEvent( |
| 96 | + 'suites', |
| 97 | + suite({ |
| 98 | + start: 100, |
| 99 | + end: 200, |
| 100 | + childState: 'failed', |
| 101 | + childError: { message: 'expected X, got Y' } |
| 102 | + }) |
| 103 | + ) |
| 104 | + |
| 105 | + const snap = baselineStore.snapshot(SUITE_UID, 'suite')! |
| 106 | + expect(snap.test.state).toBe('failed') |
| 107 | + expect(snap.test.error?.message).toBe('expected X, got Y') |
| 108 | + expect(snap.steps?.[0]).toMatchObject({ |
| 109 | + uid: TEST_UID, |
| 110 | + state: 'failed' |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + it('preserve refuses an empty-command snapshot (the 409 case)', () => { |
| 115 | + baselineStore.recordEvent('suites', suite({ start: 100, end: 200 })) |
| 116 | + expect(baselineStore.preserve(TEST_UID, 'test')).toBeUndefined() |
| 117 | + expect(baselineStore.get(TEST_UID)).toBeUndefined() |
| 118 | + }) |
| 119 | + |
| 120 | + it('preserve stores; clearAll wipes every baseline and returns their uids', () => { |
| 121 | + baselineStore.recordEvent('commands', [ |
| 122 | + { timestamp: 150, command: 'click', args: [] } |
| 123 | + ]) |
| 124 | + baselineStore.recordEvent('suites', suite({ start: 100, end: 200 })) |
| 125 | + |
| 126 | + const attempt = baselineStore.preserve(TEST_UID, 'test')! |
| 127 | + expect(baselineStore.get(TEST_UID)).toBe(attempt) |
| 128 | + |
| 129 | + expect(baselineStore.clearAll()).toEqual([TEST_UID]) |
| 130 | + expect(baselineStore.get(TEST_UID)).toBeUndefined() |
| 131 | + }) |
| 132 | +}) |
0 commit comments