const {test} = require('node:test'); const assert = require('node:assert/strict'); const {destination, resolvePath} = require('../../../static/js/browser-address.js'); test('absolute, relative and virtual paths use the browser route', () => { assert.equal(destination('/docs/new').href, '/v/browser/docs/new'); assert.equal(destination('child', '/docs').href, '/v/browser/docs/child'); assert.equal(destination('cd ../new', '/docs/old').path, '/docs/new'); assert.equal(destination('cd', '/docs').path, '/'); assert.equal(destination('cd ../../../../', '/docs').path, '/'); assert.equal(destination('./a//b/./c/..', '/docs').path, '/docs/a/b'); }); test('editor and viewer commands preserve names and avoid adapter-route ambiguity', () => { assert.equal(destination('vi plain/example', '/').href, '/e/-/plain/example'); assert.equal(destination('view', '/docs/page').href, '/docs/page'); assert.equal(destination('vi file name', '/docs').href, '/e/-/docs/file%20name'); assert.equal(destination('vi 日記 #1?%25', '/docs').href, '/e/-/docs/%E6%97%A5%E8%A8%98%20%231%3F%2525'); assert.equal(destination('/cd', '/docs').path, '/cd'); assert.equal(destination('view a;rm -rf', '/').href, '/a%3Brm%20-rf'); }); test('schemes, network paths and controls are not accepted', () => { for (const value of ['https://outside.test', '//outside.test', 'javascript:alert(1)', 'file:///etc/passwd', 'a\\b', '/a\n/b', '/a\u0000b']) { assert.throws(() => resolvePath(value), /invalid_path/); } assert.equal(destination('view /%2f%2fevil.test').href, '/%252f%252fevil.test'); });