|
| 1 | +// GH #112: regression test for sidecarPathFor's basename extraction. |
| 2 | +// The original implementation used `split('/').pop()` which returned the |
| 3 | +// entire backslash-containing path as a single segment on Windows, |
| 4 | +// producing absurd `join(parent, 'state', <full-windows-path>)` deep |
| 5 | +// directory trees. The fix splits on both separators explicitly. |
| 6 | +// |
| 7 | +// We run this on a POSIX runtime (darwin/linux) and assert that the |
| 8 | +// EXTRACTED basename is correct for Windows-style input regardless of |
| 9 | +// platform-native path module behavior. Parent-dir resolution on a |
| 10 | +// Windows path is platform-dependent and out of scope — the goal here is |
| 11 | +// only "the basename portion is correctly extracted." |
| 12 | +import { test } from 'node:test'; |
| 13 | +import assert from 'node:assert/strict'; |
| 14 | + |
| 15 | +const MOD_PATH = '../../dist/domain/sidecar-io.js'; |
| 16 | + |
| 17 | +test('sidecarPathFor: POSIX-style path produces expected sidecar path', async () => { |
| 18 | + const { sidecarPathFor } = await import(MOD_PATH); |
| 19 | + const result = sidecarPathFor('/Users/me/project/.rn-agent/actions/wizard-create-task.yaml'); |
| 20 | + assert.equal(result, '/Users/me/project/.rn-agent/state/wizard-create-task.state.json'); |
| 21 | +}); |
| 22 | + |
| 23 | +test('sidecarPathFor: .yml extension also handled', async () => { |
| 24 | + const { sidecarPathFor } = await import(MOD_PATH); |
| 25 | + const result = sidecarPathFor('/a/b/actions/short.yml'); |
| 26 | + assert.equal(result, '/a/b/state/short.state.json'); |
| 27 | +}); |
| 28 | + |
| 29 | +test('sidecarPathFor: Windows-style backslash input extracts basename correctly (no embedded path in result)', async () => { |
| 30 | + // GH #112: this is the regression case. The buggy code produced a base |
| 31 | + // string containing the entire backslash path, which join() then |
| 32 | + // embedded into a deeply-nested directory tree. The fix's contract is |
| 33 | + // that the result's filename portion is just `<id>.state.json`, not |
| 34 | + // the full path. We assert on the END of the result string so we don't |
| 35 | + // depend on platform-native dirname/join behavior for Windows inputs |
| 36 | + // on a POSIX test runtime. |
| 37 | + const { sidecarPathFor } = await import(MOD_PATH); |
| 38 | + const result = sidecarPathFor('C:\\Users\\foo\\project\\.rn-agent\\actions\\my-action.yaml'); |
| 39 | + // The trailing segment must be `my-action.state.json`, not the full |
| 40 | + // backslash-containing path. |
| 41 | + assert.match(result, /[/\\]my-action\.state\.json$/, `result did not end with clean basename: ${result}`); |
| 42 | + // The result must NOT contain the entire Windows-style filename glued in. |
| 43 | + assert.ok(!result.includes('C:\\Users\\foo\\project\\.rn-agent\\actions\\my-action.state.json'), |
| 44 | + `result still contains the full Windows path: ${result}`); |
| 45 | +}); |
| 46 | + |
| 47 | +test('sidecarPathFor: mixed forward+backward slash input extracts basename correctly', async () => { |
| 48 | + // Defensive: someone hand-builds a path with both separators (e.g. |
| 49 | + // pre-processing wasn't normalized). Should still extract the trailing |
| 50 | + // segment cleanly. |
| 51 | + const { sidecarPathFor } = await import(MOD_PATH); |
| 52 | + const result = sidecarPathFor('/a/b\\c/d\\my-action.yaml'); |
| 53 | + assert.match(result, /[/\\]my-action\.state\.json$/, `mixed-separator result: ${result}`); |
| 54 | + assert.ok(!result.includes('a/b\\c/d\\my-action.state.json')); |
| 55 | +}); |
0 commit comments