'use strict'; const assert = require('node:assert/strict'); const test = require('node:test'); const { listDirectoryWithCwd } = require('../../src/ftp'); test('FTP directory listing uses an exact CWD and restores the prior directory', async () => { const calls = []; const client = { pwd: async () => '/previous', cd: async directory => { calls.push(['cd', directory]); }, list: async () => { calls.push(['list']); return [{ name: 'app0' }]; }, }; const result = await listDirectoryWithCwd(client, '/mnt/sandbox/PPSA17330_000'); assert.deepEqual(result, [{ name: 'app0' }]); assert.deepEqual(calls, [ ['cd', '/mnt/sandbox/PPSA17330_000'], ['list'], ['cd', '/previous'], ]); }); test('FTP directory listing restores the prior directory after LIST failure', async () => { const calls = []; const client = { pwd: async () => '/previous', cd: async directory => { calls.push(['cd', directory]); }, list: async () => { calls.push(['list']); throw new Error('LIST rejected'); }, }; await assert.rejects(() => listDirectoryWithCwd(client, '/mnt/sandbox'), /LIST rejected/); assert.deepEqual(calls.at(-1), ['cd', '/previous']); });