|
| 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