|
| 1 | +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { dirname, join } from 'node:path'; |
| 4 | + |
| 5 | +let telemetryFilePath: string; |
| 6 | + |
| 7 | +vi.mock('../../../../src/lib/consts.js', async (importOriginal) => { |
| 8 | + const original = await importOriginal<typeof import('../../../../src/lib/consts.js')>(); |
| 9 | + |
| 10 | + return { |
| 11 | + ...original, |
| 12 | + TELEMETRY_FILE_PATH: () => telemetryFilePath, |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('../../../../src/lib/utils.js', () => ({ |
| 17 | + getLocalUserInfo: async () => ({}), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('../../../../src/lib/outputs.js', () => ({ |
| 21 | + info: () => { |
| 22 | + /* noop */ |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +function writeTelemetryState(state: Record<string, unknown>) { |
| 27 | + const dir = dirname(telemetryFilePath); |
| 28 | + if (!existsSync(dir)) { |
| 29 | + mkdirSync(dir, { recursive: true }); |
| 30 | + } |
| 31 | + writeFileSync(telemetryFilePath, JSON.stringify(state, null, '\t')); |
| 32 | +} |
| 33 | + |
| 34 | +function readTelemetryState() { |
| 35 | + return JSON.parse(readFileSync(telemetryFilePath, 'utf-8')); |
| 36 | +} |
| 37 | + |
| 38 | +describe('checkAndUpdateLastCommand', () => { |
| 39 | + let testDir: string; |
| 40 | + let counter = 0; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + counter++; |
| 44 | + testDir = join(tmpdir(), `apify-cli-test-telemetry-${process.pid}-${counter}-${Date.now()}`); |
| 45 | + telemetryFilePath = join(testDir, 'telemetry.json'); |
| 46 | + vi.useFakeTimers(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.useRealTimers(); |
| 51 | + // Clean up temp files |
| 52 | + if (existsSync(testDir)) { |
| 53 | + rmSync(testDir, { recursive: true, force: true }); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test('returns false on first invocation (no prior command)', async () => { |
| 58 | + vi.setSystemTime(1000); |
| 59 | + |
| 60 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 61 | + |
| 62 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 63 | + |
| 64 | + expect(result).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + test('stores the command and timestamp in telemetry state', async () => { |
| 68 | + vi.setSystemTime(50_000); |
| 69 | + |
| 70 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 71 | + |
| 72 | + await checkAndUpdateLastCommand('apify push'); |
| 73 | + |
| 74 | + const state = readTelemetryState(); |
| 75 | + expect(state.lastCommand).toBe('apify push'); |
| 76 | + expect(state.lastCommandTimestamp).toBe(50_000); |
| 77 | + }); |
| 78 | + |
| 79 | + test('returns true when the same command is repeated within the retry window', async () => { |
| 80 | + vi.setSystemTime(100_000); |
| 81 | + |
| 82 | + // Seed state with a recent identical command |
| 83 | + writeTelemetryState({ |
| 84 | + version: 1, |
| 85 | + enabled: true, |
| 86 | + anonymousId: 'CLI:test', |
| 87 | + lastCommand: 'apify run', |
| 88 | + lastCommandTimestamp: 95_000, // 5 seconds ago — within the 10s window |
| 89 | + }); |
| 90 | + |
| 91 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 92 | + |
| 93 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 94 | + |
| 95 | + expect(result).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + test('returns false when the same command is repeated outside the retry window', async () => { |
| 99 | + vi.setSystemTime(100_000); |
| 100 | + |
| 101 | + writeTelemetryState({ |
| 102 | + version: 1, |
| 103 | + enabled: true, |
| 104 | + anonymousId: 'CLI:test', |
| 105 | + lastCommand: 'apify run', |
| 106 | + lastCommandTimestamp: 80_000, // 20 seconds ago — outside the 10s window |
| 107 | + }); |
| 108 | + |
| 109 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 110 | + |
| 111 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 112 | + |
| 113 | + expect(result).toBe(false); |
| 114 | + }); |
| 115 | + |
| 116 | + test('returns false when a different command is run within the retry window', async () => { |
| 117 | + vi.setSystemTime(100_000); |
| 118 | + |
| 119 | + writeTelemetryState({ |
| 120 | + version: 1, |
| 121 | + enabled: true, |
| 122 | + anonymousId: 'CLI:test', |
| 123 | + lastCommand: 'apify run', |
| 124 | + lastCommandTimestamp: 95_000, |
| 125 | + }); |
| 126 | + |
| 127 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 128 | + |
| 129 | + const result = await checkAndUpdateLastCommand('apify push'); |
| 130 | + |
| 131 | + expect(result).toBe(false); |
| 132 | + }); |
| 133 | + |
| 134 | + test('updates state after checking so the next call sees the new command', async () => { |
| 135 | + vi.setSystemTime(100_000); |
| 136 | + |
| 137 | + writeTelemetryState({ |
| 138 | + version: 1, |
| 139 | + enabled: true, |
| 140 | + anonymousId: 'CLI:test', |
| 141 | + lastCommand: 'apify run', |
| 142 | + lastCommandTimestamp: 90_000, |
| 143 | + }); |
| 144 | + |
| 145 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 146 | + |
| 147 | + await checkAndUpdateLastCommand('apify push'); |
| 148 | + |
| 149 | + const state = readTelemetryState(); |
| 150 | + expect(state.lastCommand).toBe('apify push'); |
| 151 | + expect(state.lastCommandTimestamp).toBe(100_000); |
| 152 | + }); |
| 153 | + |
| 154 | + test('returns false when lastCommandTimestamp is missing', async () => { |
| 155 | + vi.setSystemTime(100_000); |
| 156 | + |
| 157 | + writeTelemetryState({ |
| 158 | + version: 1, |
| 159 | + enabled: true, |
| 160 | + anonymousId: 'CLI:test', |
| 161 | + lastCommand: 'apify run', |
| 162 | + // no lastCommandTimestamp |
| 163 | + }); |
| 164 | + |
| 165 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 166 | + |
| 167 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 168 | + |
| 169 | + expect(result).toBe(false); |
| 170 | + }); |
| 171 | + |
| 172 | + test('returns false when telemetry state file is corrupted', async () => { |
| 173 | + // Write invalid JSON |
| 174 | + const dir = dirname(telemetryFilePath); |
| 175 | + mkdirSync(dir, { recursive: true }); |
| 176 | + writeFileSync(telemetryFilePath, '{{{invalid json'); |
| 177 | + |
| 178 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 179 | + |
| 180 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 181 | + |
| 182 | + expect(result).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + test('returns true at exactly the retry window boundary', async () => { |
| 186 | + // Command was run exactly 9999ms ago (just inside the 10_000ms window) |
| 187 | + vi.setSystemTime(109_999); |
| 188 | + |
| 189 | + writeTelemetryState({ |
| 190 | + version: 1, |
| 191 | + enabled: true, |
| 192 | + anonymousId: 'CLI:test', |
| 193 | + lastCommand: 'apify run', |
| 194 | + lastCommandTimestamp: 100_000, |
| 195 | + }); |
| 196 | + |
| 197 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 198 | + |
| 199 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 200 | + |
| 201 | + expect(result).toBe(true); |
| 202 | + }); |
| 203 | + |
| 204 | + test('returns false at exactly the retry window boundary (equal to window)', async () => { |
| 205 | + // Command was run exactly 10_000ms ago (at the boundary, not strictly less than) |
| 206 | + vi.setSystemTime(110_000); |
| 207 | + |
| 208 | + writeTelemetryState({ |
| 209 | + version: 1, |
| 210 | + enabled: true, |
| 211 | + anonymousId: 'CLI:test', |
| 212 | + lastCommand: 'apify run', |
| 213 | + lastCommandTimestamp: 100_000, |
| 214 | + }); |
| 215 | + |
| 216 | + const { checkAndUpdateLastCommand } = await import('../../../../src/lib/hooks/telemetry/useTelemetryState.js'); |
| 217 | + |
| 218 | + const result = await checkAndUpdateLastCommand('apify run'); |
| 219 | + |
| 220 | + expect(result).toBe(false); |
| 221 | + }); |
| 222 | +}); |
0 commit comments