|
| 1 | +# Update TestTerminalContext to integrate with Terminal.Instance property |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +Update the existing `TestTerminalContext` to provide seamless integration with the new `Terminal.Instance` static property. This enables users to use the ambient test context pattern while also having access to the static `Terminal` class methods in tests. |
| 6 | + |
| 7 | +## Checklist |
| 8 | + |
| 9 | +- [ ] Add `Terminal` property to `TestTerminalContext` that returns `TestTerminal` |
| 10 | +- [ ] Modify `TestTerminalContext.Resolve(ITerminal? terminal)` to check `Terminal.Instance` first |
| 11 | +- [ ] Update `TestTerminalContext` to set `Terminal.Instance = Context.Terminal` on initialization |
| 12 | +- [ ] Restore previous `Terminal.Instance` on disposal (for proper test isolation) |
| 13 | +- [ ] Update XML documentation with test usage patterns |
| 14 | +- [ ] Write integration tests demonstrating the test pattern |
| 15 | +- [ ] Update existing tests to use the new static API |
| 16 | + |
| 17 | +## Notes |
| 18 | + |
| 19 | +### Current Status (2026-03-19) |
| 20 | + |
| 21 | +**What exists:** |
| 22 | +- `TestTerminalContext` class in `source/timewarp-terminal/test-terminal-context.cs` |
| 23 | + - Uses `AsyncLocal<TestTerminal?>` for test isolation |
| 24 | + - Has `Current` property and `Resolve()` methods |
| 25 | + - **NOT integrated with `Terminal.Instance`** |
| 26 | +- `Terminal` static class in `source/timewarp-terminal/terminal-static.cs` |
| 27 | + - Has `Instance` property that can be set to any `ITerminal` |
| 28 | + - **Does NOT check `TestTerminalContext.Current`** |
| 29 | + |
| 30 | +**Current test pattern (working):** |
| 31 | +```csharp |
| 32 | +ITerminal original = Terminal.Instance; |
| 33 | +using TestTerminal testTerminal = new(); |
| 34 | +try |
| 35 | +{ |
| 36 | + Terminal.Instance = testTerminal; |
| 37 | + // test code |
| 38 | +} |
| 39 | +finally |
| 40 | +{ |
| 41 | + Terminal.Instance = original; |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +**Proposed pattern (cleaner):** |
| 46 | +```csharp |
| 47 | +using TestTerminal terminal = new(); |
| 48 | +TestTerminalContext.Current = terminal; |
| 49 | +// Terminal.Instance automatically set |
| 50 | +// Terminal.WriteLine routes to testTerminal |
| 51 | +// Automatic restoration on dispose |
| 52 | +``` |
| 53 | + |
| 54 | +### Why This Task |
| 55 | + |
| 56 | +The integration would provide: |
| 57 | +1. **Cleaner test code** - No manual try/finally blocks |
| 58 | +2. **Automatic restoration** - `Terminal.Instance` restored when context disposed |
| 59 | +3. **AsyncLocal isolation** - Each async context gets its own terminal |
| 60 | +4. **Backward compatibility** - Existing pattern still works |
| 61 | + |
| 62 | +### Files to Modify |
| 63 | + |
| 64 | +- `source/timewarp-terminal/test-terminal-context.cs` - Add integration logic |
| 65 | +- `tests/*.cs` - Optionally update to use new pattern (low priority) |
0 commit comments