Skip to content

Commit 05f1bc6

Browse files
committed
fixing tests
1 parent 6e8a35a commit 05f1bc6

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/services/storage/backupRestoreService.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ import { runMigrations, listMigrationFiles } from './migrations/migrate.js';
1919
let _AdmZipSingleton = null;
2020
async function getAdmZip() {
2121
if (_AdmZipSingleton) return _AdmZipSingleton;
22+
// Allow tests to provide a mock constructor without ESM loader intricacies
23+
if (globalThis && globalThis.__TEST_ADM_ZIP__) {
24+
_AdmZipSingleton = globalThis.__TEST_ADM_ZIP__;
25+
return _AdmZipSingleton;
26+
}
2227
const mod = await import('adm-zip');
23-
_AdmZipSingleton = mod.default || mod;
28+
_AdmZipSingleton = (mod && mod.default) || mod;
2429
return _AdmZipSingleton;
2530
}
2631

test/backup/backupRestoreService.test.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ import esmock from 'esmock';
88

99
describe('services/storage/backupRestoreService.js - precheck & filename', () => {
1010
let svc;
11-
let admZipMock;
11+
let setZipState;
1212
let calls;
1313

1414
beforeEach(async () => {
1515
calls = { logger: { info: [], warn: [], error: [] } };
1616

17-
// Mock AdmZip with configurable state
18-
let state = { hasDb: false, meta: null };
17+
// Mock AdmZip with configurable state via globalThis (avoid esmock export name pitfalls)
18+
globalThis.__ADM_ZIP_STATE__ = { hasDb: false, meta: null };
19+
setZipState = (s) => {
20+
globalThis.__ADM_ZIP_STATE__ = { ...globalThis.__ADM_ZIP_STATE__, ...s };
21+
};
1922
class MockAdmZip {
2023
constructor() {}
2124
getEntry(name) {
25+
const state = globalThis.__ADM_ZIP_STATE__ || {};
2226
if (name === 'listings.db') {
2327
if (state.hasDb) return { getData: () => Buffer.from('db') };
2428
return null;
@@ -30,12 +34,15 @@ describe('services/storage/backupRestoreService.js - precheck & filename', () =>
3034
return null;
3135
}
3236
getEntries() {
37+
const state = globalThis.__ADM_ZIP_STATE__ || {};
3338
const arr = [];
3439
if (state.hasDb) arr.push({ entryName: 'listings.db', getData: () => Buffer.from('db') });
3540
return arr;
3641
}
3742
}
38-
admZipMock = { default: MockAdmZip, __set: (s) => (state = { ...state, ...s }) };
43+
const admZipMock = { default: MockAdmZip };
44+
// Also expose for service via globalThis escape hatch
45+
globalThis.__TEST_ADM_ZIP__ = MockAdmZip;
3946

4047
const path = await import('node:path');
4148
const ROOT = path.resolve('.');
@@ -70,11 +77,13 @@ describe('services/storage/backupRestoreService.js - precheck & filename', () =>
7077

7178
const utilsMock = { getPackageVersion: async () => '16.2.0' };
7279

80+
const admZipPath = path.join(ROOT, 'node_modules', 'adm-zip', 'adm-zip.js');
7381
const mod = await esmock(
7482
path.join(ROOT, 'lib', 'services', 'storage', 'backupRestoreService.js'),
7583
{},
7684
{
7785
'adm-zip': admZipMock,
86+
[admZipPath]: admZipMock,
7887
[migratePath]: migrateMock,
7988
[sqlitePath]: sqliteMock,
8089
[loggerPath]: loggerMock,
@@ -94,15 +103,15 @@ describe('services/storage/backupRestoreService.js - precheck & filename', () =>
94103
});
95104

96105
it('precheck: missing listings.db yields danger', async () => {
97-
admZipMock.__set({ hasDb: false, meta: { dbMigration: 9 } });
106+
setZipState({ hasDb: false, meta: { dbMigration: 9 } });
98107
const res = await svc.precheckRestore(Buffer.from('dummy'));
99108
expect(res.compatible).to.equal(false);
100109
expect(res.severity).to.equal('danger');
101110
expect(res.message).to.match(/missing the database file/i);
102111
});
103112

104113
it('precheck: older backup is compatible with warning', async () => {
105-
admZipMock.__set({ hasDb: true, meta: { dbMigration: 5, fredyVersion: '16.0.0' } });
114+
setZipState({ hasDb: true, meta: { dbMigration: 5, fredyVersion: '16.0.0' } });
106115
const res = await svc.precheckRestore(Buffer.from('zip'));
107116
expect(res.compatible).to.equal(true);
108117
expect(res.severity).to.equal('warning');
@@ -112,14 +121,14 @@ describe('services/storage/backupRestoreService.js - precheck & filename', () =>
112121
});
113122

114123
it('precheck: equal backup is compatible with info', async () => {
115-
admZipMock.__set({ hasDb: true, meta: { dbMigration: 10 } });
124+
setZipState({ hasDb: true, meta: { dbMigration: 10 } });
116125
const res = await svc.precheckRestore(Buffer.from('zip'));
117126
expect(res.compatible).to.equal(true);
118127
expect(res.severity).to.equal('info');
119128
});
120129

121130
it('precheck: newer backup yields danger', async () => {
122-
admZipMock.__set({ hasDb: true, meta: { dbMigration: 11 } });
131+
setZipState({ hasDb: true, meta: { dbMigration: 11 } });
123132
const res = await svc.precheckRestore(Buffer.from('zip'));
124133
expect(res.compatible).to.equal(false);
125134
expect(res.severity).to.equal('danger');

0 commit comments

Comments
 (0)