|
| 1 | +import { |
| 2 | + Configuration, |
| 3 | + type Path, |
| 4 | + PathReplacer, |
| 5 | + ProcessBuilder, |
| 6 | + TeamcityEvent, |
| 7 | + type TestFailed, |
| 8 | + type TestFinished, |
| 9 | + type TestIgnored, |
| 10 | + type TestStarted, |
| 11 | +} from '@vscode-phpunit/phpunit'; |
| 12 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 13 | +import type { TestItem } from 'vscode'; |
| 14 | +import * as vscode from 'vscode'; |
| 15 | +import { DebugOutputObserver } from './DebugOutputObserver'; |
| 16 | + |
| 17 | +const createTestItem = (id: string): TestItem => ({ id }) as TestItem; |
| 18 | + |
| 19 | +const createBuilder = (command = 'php') => { |
| 20 | + const config = new Configuration({ php: command }); |
| 21 | + const options = { cwd: '.' }; |
| 22 | + return new ProcessBuilder( |
| 23 | + config, |
| 24 | + options, |
| 25 | + new PathReplacer(options, config.get('paths') as Path), |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +const makeStarted = (id: string, locationHint: string, file: string): TestStarted => ({ |
| 30 | + event: TeamcityEvent.testStarted, |
| 31 | + name: id.split('::').pop() ?? id, |
| 32 | + flowId: 1, |
| 33 | + id, |
| 34 | + file, |
| 35 | + locationHint, |
| 36 | +}); |
| 37 | + |
| 38 | +const makeFinished = (id: string, locationHint: string, file: string): TestFinished => ({ |
| 39 | + event: TeamcityEvent.testFinished, |
| 40 | + name: id.split('::').pop() ?? id, |
| 41 | + flowId: 1, |
| 42 | + id, |
| 43 | + file, |
| 44 | + locationHint, |
| 45 | + duration: 10, |
| 46 | +}); |
| 47 | + |
| 48 | +const makeFailed = (id: string, locationHint: string, file: string): TestFailed => ({ |
| 49 | + event: TeamcityEvent.testFailed, |
| 50 | + name: id.split('::').pop() ?? id, |
| 51 | + flowId: 1, |
| 52 | + id, |
| 53 | + file, |
| 54 | + locationHint, |
| 55 | + message: 'assertion failed', |
| 56 | + details: [], |
| 57 | + duration: 10, |
| 58 | +}); |
| 59 | + |
| 60 | +const makeIgnored = (id: string, locationHint: string, file: string): TestIgnored => ({ |
| 61 | + event: TeamcityEvent.testIgnored, |
| 62 | + name: id.split('::').pop() ?? id, |
| 63 | + flowId: 1, |
| 64 | + id, |
| 65 | + file, |
| 66 | + locationHint, |
| 67 | + message: 'skipped', |
| 68 | + details: [], |
| 69 | + duration: 0, |
| 70 | +}); |
| 71 | + |
| 72 | +describe('DebugOutputObserver', () => { |
| 73 | + const ID = 'Tests\\Feature\\ExampleTest::test_foo'; |
| 74 | + const LOCATION_HINT = `php_qn:///var/www/html/tests/Feature/ExampleTest.php::Tests\\Feature\\ExampleTest::test_foo`; |
| 75 | + const FILE = '/local/tests/Feature/ExampleTest.php'; |
| 76 | + |
| 77 | + // biome-ignore lint/suspicious/noExplicitAny: test mock type |
| 78 | + let outputChannel: any; |
| 79 | + let configuration: Configuration; |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + outputChannel = vscode.window.createOutputChannel('PHPUnit Debug'); |
| 83 | + configuration = new Configuration({ clearDebugOutputOnRun: true }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('run()', () => { |
| 87 | + it('logs the command', () => { |
| 88 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 89 | + observer.run(createBuilder('php')); |
| 90 | + |
| 91 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('php')); |
| 92 | + }); |
| 93 | + |
| 94 | + it('logs queue keys when testItemById is not empty', () => { |
| 95 | + const testItemById = new Map([[ID, createTestItem(ID)]]); |
| 96 | + const observer = new DebugOutputObserver(outputChannel, configuration, testItemById); |
| 97 | + observer.run(createBuilder('php')); |
| 98 | + |
| 99 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 100 | + expect.stringContaining('[Queue]'), |
| 101 | + ); |
| 102 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining(ID)); |
| 103 | + }); |
| 104 | + |
| 105 | + it('does not log queue section when testItemById is empty', () => { |
| 106 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 107 | + observer.run(createBuilder('php')); |
| 108 | + |
| 109 | + expect(outputChannel.appendLine).not.toHaveBeenCalledWith( |
| 110 | + expect.stringContaining('[Queue]'), |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('clears output once per request when clearDebugOutputOnRun is true', () => { |
| 115 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 116 | + observer.run(createBuilder('cmd-1')); |
| 117 | + observer.run(createBuilder('cmd-2')); |
| 118 | + |
| 119 | + expect(outputChannel.clear).toHaveBeenCalledTimes(1); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('testStarted()', () => { |
| 124 | + it('logs ✓ found when testItem exists', () => { |
| 125 | + const testItemById = new Map([[ID, createTestItem(ID)]]); |
| 126 | + const observer = new DebugOutputObserver(outputChannel, configuration, testItemById); |
| 127 | + observer.testStarted?.(makeStarted(ID, LOCATION_HINT, FILE)); |
| 128 | + |
| 129 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 130 | + expect.stringContaining('[testStarted]'), |
| 131 | + ); |
| 132 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✓')); |
| 133 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining(ID)); |
| 134 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 135 | + expect.stringContaining(LOCATION_HINT), |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('logs ✗ not found when testItem missing', () => { |
| 140 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 141 | + observer.testStarted?.(makeStarted(ID, LOCATION_HINT, FILE)); |
| 142 | + |
| 143 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 144 | + expect.stringContaining('[testStarted]'), |
| 145 | + ); |
| 146 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✗')); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('testFinished()', () => { |
| 151 | + it('logs ✓ found when testItem exists', () => { |
| 152 | + const testItemById = new Map([[ID, createTestItem(ID)]]); |
| 153 | + const observer = new DebugOutputObserver(outputChannel, configuration, testItemById); |
| 154 | + observer.testFinished?.(makeFinished(ID, LOCATION_HINT, FILE)); |
| 155 | + |
| 156 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 157 | + expect.stringContaining('[testFinished]'), |
| 158 | + ); |
| 159 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✓')); |
| 160 | + }); |
| 161 | + |
| 162 | + it('logs ✗ not found when testItem missing', () => { |
| 163 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 164 | + observer.testFinished?.(makeFinished(ID, LOCATION_HINT, FILE)); |
| 165 | + |
| 166 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 167 | + expect.stringContaining('[testFinished]'), |
| 168 | + ); |
| 169 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✗')); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('testFailed()', () => { |
| 174 | + it('logs ✗ not found when testItem missing', () => { |
| 175 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 176 | + observer.testFailed?.(makeFailed(ID, LOCATION_HINT, FILE)); |
| 177 | + |
| 178 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 179 | + expect.stringContaining('[testFailed]'), |
| 180 | + ); |
| 181 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✗')); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('testIgnored()', () => { |
| 186 | + it('logs ✗ not found when testItem missing', () => { |
| 187 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 188 | + observer.testIgnored?.(makeIgnored(ID, LOCATION_HINT, FILE)); |
| 189 | + |
| 190 | + expect(outputChannel.appendLine).toHaveBeenCalledWith( |
| 191 | + expect.stringContaining('[testIgnored]'), |
| 192 | + ); |
| 193 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining('✗')); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe('line()', () => { |
| 198 | + it('does not implement line handler', () => { |
| 199 | + const observer = new DebugOutputObserver(outputChannel, configuration, new Map()); |
| 200 | + |
| 201 | + expect('line' in observer).toBe(false); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments