Skip to content

Commit 533f643

Browse files
committed
Adding more code coverage in our tests
1 parent 87803ae commit 533f643

20 files changed

Lines changed: 4056 additions & 763 deletions

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@
279279
"lint:json": "node scripts/validate-json.js",
280280
"test": "vscode-test",
281281
"test:node": "npm run compile-tests && node --require ./out/test/unit/vscode-shim-register.js --test out/test/unit/*.test.js",
282-
"test:coverage": "npm run compile-tests && node --require ./out/test/unit/vscode-shim-register.js --experimental-test-coverage --test --test-coverage-lines=60 --test-coverage-functions=60 --test-coverage-branches=60 --test-coverage-include=out/src/backend/**/*.js --test-coverage-include=out/src/utils/**/*.js out/test/unit/backend-identity.test.js out/test/unit/utils-errors.test.js out/test/unit/backend-settings.test.js out/test/unit/backend-copyConfig.test.js out/test/unit/backend-integration.test.js out/test/unit/backend-commands.test.js out/test/unit/backend-facade-helpers.test.js out/test/unit/backend-facade-rollups.test.js out/test/unit/backend-facade-query.test.js",
282+
"test:coverage": "npm run compile-tests && node --require ./out/test/unit/vscode-shim-register.js --experimental-test-coverage --test --test-force-exit --test-coverage-lines=80 --test-coverage-functions=80 --test-coverage-branches=60 --test-coverage-include=out/src/backend/**/*.js --test-coverage-include=out/src/utils/**/*.js out/test/unit/backend-identity.test.js out/test/unit/utils-errors.test.js out/test/unit/backend-settings.test.js out/test/unit/backend-copyConfig.test.js out/test/unit/backend-integration.test.js out/test/unit/backend-commands.test.js out/test/unit/backend-facade-helpers.test.js out/test/unit/backend-facade-rollups.test.js out/test/unit/backend-facade-query.test.js out/test/unit/backend-sharingProfile.test.js out/test/unit/backend-ui-messages.test.js out/test/unit/utils-html.test.js out/test/unit/backend-configurator.test.js out/test/unit/credentialService.test.js out/test/unit/backend-displayNames.test.js out/test/unit/backend-configurationFlow.test.js out/test/unit/backend-rollups.test.js out/test/unit/backend-blobUploadService.test.js out/test/unit/backend-dataPlaneService.test.js out/test/unit/backend-utilityService.test.js out/test/unit/backend-configPanel.test.js out/test/unit/backend-syncService.test.js out/test/unit/azureResourceService.test.js out/test/unit/backend-cache-integration.test.js out/test/unit/backend-facade-methods.test.js out/test/unit/backend-redaction.test.js out/test/unit/backend-sync-profiles.test.js out/test/unit/utils-dayKeys.test.js out/test/unit/backend-queryService.test.js",
283283
"pre-release": "node scripts/pre-release.js",
284284
"capture-screenshots": "pwsh -File scripts/capture-screenshots.ps1",
285285
"sync-changelog": "node scripts/sync-changelog.js",
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import './vscode-shim-register';
2+
import test from 'node:test';
3+
import * as assert from 'node:assert/strict';
4+
5+
import * as vscode from 'vscode';
6+
import { BlobUploadService, type BlobUploadSettings } from '../../src/backend/services/blobUploadService';
7+
8+
function makeGlobalState(): vscode.Memento & { setKeysForSync?(keys: readonly string[]): void } {
9+
const state = new Map<string, unknown>();
10+
return {
11+
keys: () => [...state.keys()],
12+
get<T>(key: string, fallback?: T): T {
13+
return (state.has(key) ? state.get(key) : fallback) as T;
14+
},
15+
async update(key: string, value: unknown) {
16+
state.set(key, value);
17+
}
18+
};
19+
}
20+
21+
function makeContext(): vscode.ExtensionContext {
22+
return {
23+
globalState: makeGlobalState()
24+
} as unknown as vscode.ExtensionContext;
25+
}
26+
27+
const enabledSettings: BlobUploadSettings = {
28+
enabled: true,
29+
containerName: 'logs',
30+
uploadFrequencyHours: 24,
31+
compressFiles: true
32+
};
33+
34+
// ── shouldUpload ─────────────────────────────────────────────────────────
35+
36+
test('shouldUpload returns false when disabled', () => {
37+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
38+
assert.equal(svc.shouldUpload('m1', { ...enabledSettings, enabled: false }), false);
39+
});
40+
41+
test('shouldUpload returns true on first upload (no status)', () => {
42+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
43+
assert.equal(svc.shouldUpload('m1', enabledSettings), true);
44+
});
45+
46+
test('shouldUpload returns false when within frequency window', () => {
47+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
48+
// Simulate a recent upload by directly accessing the internal status
49+
(svc as any).uploadStatus.set('m1', {
50+
lastUploadTime: Date.now() - (1000 * 60 * 30), // 30 minutes ago
51+
filesUploaded: 5
52+
});
53+
assert.equal(svc.shouldUpload('m1', enabledSettings), false);
54+
});
55+
56+
test('shouldUpload returns true when past frequency window', () => {
57+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
58+
(svc as any).uploadStatus.set('m1', {
59+
lastUploadTime: Date.now() - (1000 * 60 * 60 * 25), // 25 hours ago
60+
filesUploaded: 5
61+
});
62+
assert.equal(svc.shouldUpload('m1', enabledSettings), true);
63+
});
64+
65+
test('shouldUpload uses custom frequency from settings', () => {
66+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
67+
(svc as any).uploadStatus.set('m1', {
68+
lastUploadTime: Date.now() - (1000 * 60 * 60 * 2), // 2 hours ago
69+
filesUploaded: 5
70+
});
71+
assert.equal(svc.shouldUpload('m1', { ...enabledSettings, uploadFrequencyHours: 1 }), true);
72+
assert.equal(svc.shouldUpload('m1', { ...enabledSettings, uploadFrequencyHours: 4 }), false);
73+
});
74+
75+
// ── getUploadStatus / clearUploadStatus ──────────────────────────────────
76+
77+
test('getUploadStatus returns undefined for unknown machineId', () => {
78+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
79+
assert.equal(svc.getUploadStatus('unknown'), undefined);
80+
});
81+
82+
test('clearUploadStatus removes all status entries', () => {
83+
const svc = new BlobUploadService(() => {}, () => {}, makeContext());
84+
(svc as any).uploadStatus.set('m1', { lastUploadTime: Date.now(), filesUploaded: 1 });
85+
svc.clearUploadStatus();
86+
assert.equal(svc.getUploadStatus('m1'), undefined);
87+
});
88+
89+
// ── constructor: loadUploadStatus ────────────────────────────────────────
90+
91+
test('constructor loads upload status from globalState', () => {
92+
const ctx = makeContext();
93+
const storedStatus = {
94+
'm1': { lastUploadTime: 1000000, filesUploaded: 10 },
95+
'm2': { lastUploadTime: 2000000, filesUploaded: 5 }
96+
};
97+
(ctx.globalState as any).update('blobUploadStatus', storedStatus);
98+
99+
const svc = new BlobUploadService(() => {}, () => {}, ctx);
100+
const status = svc.getUploadStatus('m1');
101+
assert.ok(status);
102+
assert.equal(status!.filesUploaded, 10);
103+
});
104+
105+
test('constructor filters out entries with zero filesUploaded', () => {
106+
const ctx = makeContext();
107+
const storedStatus = {
108+
'm1': { lastUploadTime: 1000000, filesUploaded: 0 },
109+
'm2': { lastUploadTime: 2000000, filesUploaded: 5 }
110+
};
111+
(ctx.globalState as any).update('blobUploadStatus', storedStatus);
112+
113+
const svc = new BlobUploadService(() => {}, () => {}, ctx);
114+
assert.equal(svc.getUploadStatus('m1'), undefined);
115+
assert.ok(svc.getUploadStatus('m2'));
116+
});
117+
118+
test('constructor handles missing context gracefully', () => {
119+
const svc = new BlobUploadService(() => {}, () => {}, undefined as any);
120+
assert.equal(svc.getUploadStatus('m1'), undefined);
121+
});

0 commit comments

Comments
 (0)