You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a migrated file grows large, split it into a subdirectory with smaller modules and an `index.ts` barrel. The `postgres/ops` migration used this pattern:
417
+
418
+
- **Move + split**:
419
+
- `postgres/ops.ts` → `postgres/ops/backup.ts` and `postgres/ops/restore.ts`
420
+
- Common types/helpers → `postgres/ops/utils.ts`
421
+
- `postgres/ops/index.ts` re-exports `backup`, `restore`, and `utils`
422
+
- **Update extender**:
423
+
- `postgres-ops.ts` should import from `./postgres/ops` (directory barrel), not a single file
424
+
- **Barrel + module resolution**:
425
+
- Keep `postgres/ops/index.ts` as the public entry so `./postgres/ops` resolves to the directory (Node/TS will prefer `index.ts`)
426
+
- This preserves existing import paths while allowing internal files to move under `postgres/ops/`
427
+
- **Split tests**:
428
+
- `postgres/ops.test.ts` → `postgres/ops/backup.test.ts` and `postgres/ops/restore.test.ts`
429
+
- Keep tests close to their modules for easier review and targeted runs
430
+
431
+
**Mocking and test patterns used in ops:**
432
+
433
+
- **Mock `execute_code`** to avoid running shell commands:
434
+
```ts
435
+
jest.mock("@cocalc/backend/misc_node", () => ({
436
+
execute_code: jest.fn(),
437
+
}));
438
+
const executeCode = execute_code as jest.MockedFunction<typeof execute_code>;
439
+
```
440
+
- **Mock `fs.readdirSync`** (non-configurable in Jest unless mocked at module level):
441
+
```ts
442
+
jest.mock("fs", () => ({
443
+
...jest.requireActual("fs"),
444
+
readdirSync: jest.fn(),
445
+
}));
446
+
const readdirSync = fs.readdirSync as unknown as jest.MockedFunction<
447
+
(path: fs.PathLike) => string[]
448
+
>;
449
+
```
450
+
- **Callback typing**: when stubbing callback-style APIs, pass `undefined` explicitly:
451
+
```ts
452
+
executeCode.mockImplementation((opts) => {
453
+
opts.cb?.(undefined);
454
+
});
455
+
```
456
+
457
+
Suggested test runs for the split ops tests:
458
+
459
+
```bash
460
+
pnpm test postgres/ops/backup.test.ts postgres/ops/restore.test.ts
461
+
```
462
+
463
+
#### 2.4 Method Migration Priority
415
464
416
465
**Order of migration** (from foundational to dependent):
0 commit comments