Skip to content

Commit faa5278

Browse files
committed
tests: run the isValidPadId regression under the mocha suite
The original unit test lived in the vitest backend-new suite, but PadManager loads DB, Pad and customError with CommonJS require() at import time. Under vitest those require() calls are resolved by Node natively and fail on the .ts sources ("Cannot find module '../utils/customError'"); vi.mock could not intercept them, so the suite errored before any test ran. It also used a top-level `await import`, which tripped tsc TS1378 under the project tsconfig. Move the test to the mocha backend suite, which runs with --import=tsx and resolves the .ts requires natively, so PadManager can be required directly with no mocking. isValidPadId is a pure function and DB only connects lazily in DB.init(), so loading the module has no side effects and no database is needed.
1 parent 9e5176d commit faa5278

2 files changed

Lines changed: 48 additions & 56 deletions

File tree

src/tests/backend-new/specs/PadManager.test.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
// Unit coverage for PadManager.isValidPadId.
4+
//
5+
// isValidPadId is a pure function (a regex test), so this spec just requires
6+
// PadManager and exercises it directly — no running database is needed.
7+
// PadManager's import-time `require`s (DB, Pad, customError) only *define*
8+
// things; the database connection happens lazily in DB.init(), so loading the
9+
// module here has no side effects. This runs under the mocha backend suite
10+
// (`--import=tsx`), where `require()` resolves the `.ts` sources natively.
11+
12+
import {strict as assert} from 'assert';
13+
14+
const padManager = require('../../../node/db/PadManager');
15+
16+
describe(__filename, function () {
17+
describe('isValidPadId', function () {
18+
it('accepts ordinary pad ids', async function () {
19+
for (const id of [
20+
'foo',
21+
'TF-EVC',
22+
'TF-LEC_IP03-EMS-CSM',
23+
'a.b',
24+
'.foo',
25+
'foo.',
26+
"a'b",
27+
'g.s8oes9dhwrvt0zif$bar', // group pad
28+
]) {
29+
assert.equal(padManager.isValidPadId(id), true, `expected "${id}" to be valid`);
30+
}
31+
});
32+
33+
// Regression test for "Cannot GET /p/": a pad id that is a URL dot-segment
34+
// ('.' or '..') is normalised away by the browser per the WHATWG URL
35+
// standard ('/p/.' -> '/p/', '/p/..' -> '/'), so the pad can never be
36+
// opened or exported. Such ids must be rejected. Before the fix
37+
// isValidPadId returned true for both, so this test would fail.
38+
it('rejects URL dot-segment pad ids that would be unreachable', async function () {
39+
assert.equal(padManager.isValidPadId('.'), false);
40+
assert.equal(padManager.isValidPadId('..'), false);
41+
});
42+
43+
it('still rejects empty ids and ids containing "$"', async function () {
44+
assert.equal(padManager.isValidPadId(''), false);
45+
assert.equal(padManager.isValidPadId('a$b'), false);
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)