|
| 1 | +import { mkdtempSync, readFileSync, writeFileSync, rmSync, utimesSync, existsSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import type { CacheKeyInput } from '../src/services/cache' |
| 6 | + |
| 7 | +type CacheModule = typeof import('../src/services/cache') |
| 8 | + |
| 9 | +// `src/services/cache.ts` captures `homedir()` and `CACHE_DIR` at module load |
| 10 | +// time. We therefore need to mutate HOME and then dynamically import the |
| 11 | +// module so the cache lands under the test sandbox instead of the real user |
| 12 | +// home directory. |
| 13 | +describe('CacheService', () => { |
| 14 | + const originalHome = process.env.HOME |
| 15 | + const originalUserProfile = process.env.USERPROFILE |
| 16 | + let sandbox: string |
| 17 | + let cache: CacheModule |
| 18 | + |
| 19 | + const input: CacheKeyInput = { |
| 20 | + url: 'https://shelve.cloud', |
| 21 | + teamSlug: 'acme', |
| 22 | + projectName: 'web', |
| 23 | + environmentName: 'production', |
| 24 | + } |
| 25 | + const token = 'she_live_'.concat('a'.repeat(40)) |
| 26 | + const variables = [ |
| 27 | + { key: 'DATABASE_URL', value: 'postgres://prod' }, |
| 28 | + { key: 'API_KEY', value: 'sk-abc' }, |
| 29 | + ] |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + sandbox = mkdtempSync(join(tmpdir(), 'shelve-cache-')) |
| 33 | + process.env.HOME = sandbox |
| 34 | + process.env.USERPROFILE = sandbox |
| 35 | + vi.resetModules() |
| 36 | + cache = await import('../src/services/cache') |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + rmSync(sandbox, { recursive: true, force: true }) |
| 41 | + process.env.HOME = originalHome |
| 42 | + process.env.USERPROFILE = originalUserProfile |
| 43 | + }) |
| 44 | + |
| 45 | + it('writes and reads back variables with the same token', () => { |
| 46 | + cache.CacheService.write(input, token, variables) |
| 47 | + const read = cache.CacheService.read(input, token, 60_000) |
| 48 | + expect(read).toEqual(variables) |
| 49 | + }) |
| 50 | + |
| 51 | + it('returns null when the file does not exist', () => { |
| 52 | + expect(cache.CacheService.read(input, token, 60_000)).toBeNull() |
| 53 | + }) |
| 54 | + |
| 55 | + it('refuses to write without a token', () => { |
| 56 | + cache.CacheService.write(input, '', variables) |
| 57 | + expect(cache.CacheService.read(input, token, 60_000)).toBeNull() |
| 58 | + }) |
| 59 | + |
| 60 | + it('returns null when reading without a token', () => { |
| 61 | + cache.CacheService.write(input, token, variables) |
| 62 | + expect(cache.CacheService.read(input, '', 60_000)).toBeNull() |
| 63 | + }) |
| 64 | + |
| 65 | + it('fails decryption (returns null) when the token changes', () => { |
| 66 | + cache.CacheService.write(input, token, variables) |
| 67 | + const tampered = cache.CacheService.read(input, 'she_live_'.concat('b'.repeat(40)), 60_000) |
| 68 | + expect(tampered).toBeNull() |
| 69 | + }) |
| 70 | + |
| 71 | + it('respects TTL: stale entries are not returned', () => { |
| 72 | + cache.CacheService.write(input, token, variables) |
| 73 | + const file = cache.cacheFilePath(input) |
| 74 | + const past = (Date.now() - 60 * 60 * 1000) / 1000 |
| 75 | + utimesSync(file, past, past) |
| 76 | + expect(cache.CacheService.read(input, token, 60_000)).toBeNull() |
| 77 | + }) |
| 78 | + |
| 79 | + it('returns the payload when ttlMs is 0 (no expiry)', () => { |
| 80 | + cache.CacheService.write(input, token, variables) |
| 81 | + const file = cache.cacheFilePath(input) |
| 82 | + const past = (Date.now() - 365 * 24 * 60 * 60 * 1000) / 1000 |
| 83 | + utimesSync(file, past, past) |
| 84 | + const read = cache.CacheService.read(input, token, 0) |
| 85 | + expect(read).toEqual(variables) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns null when the ciphertext is tampered with', () => { |
| 89 | + cache.CacheService.write(input, token, variables) |
| 90 | + const file = cache.cacheFilePath(input) |
| 91 | + expect(existsSync(file)).toBe(true) |
| 92 | + const blob = readFileSync(file) |
| 93 | + blob[blob.length - 1] = (blob[blob.length - 1] ?? 0) ^ 0xff |
| 94 | + writeFileSync(file, blob) |
| 95 | + expect(cache.CacheService.read(input, token, 60_000)).toBeNull() |
| 96 | + }) |
| 97 | + |
| 98 | + it('produces different cache keys for different envs', () => { |
| 99 | + const a = cache.cacheFilePath(input) |
| 100 | + const b = cache.cacheFilePath({ ...input, environmentName: 'staging' }) |
| 101 | + expect(a).not.toBe(b) |
| 102 | + }) |
| 103 | + |
| 104 | + it('ignores trailing slashes in the base URL for the cache key', () => { |
| 105 | + const a = cache.cacheFilePath(input) |
| 106 | + const b = cache.cacheFilePath({ ...input, url: 'https://shelve.cloud/' }) |
| 107 | + expect(a).toBe(b) |
| 108 | + }) |
| 109 | +}) |
0 commit comments