|
| 1 | +// @vitest-environment node |
| 2 | +import { describe, expect, it, beforeEach, afterEach } from 'vitest' |
| 3 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { resolve } from 'node:path' |
| 6 | +import { |
| 7 | + TypeScriptCDNCache, |
| 8 | + TypeScriptMemoryCache, |
| 9 | + type TypeScriptCacheInterface |
| 10 | +} from './typescript-cache' |
| 11 | +import { |
| 12 | + configureTestServer, |
| 13 | + resetTestServer, |
| 14 | + testConfigs |
| 15 | +} from '../mocks/test-setup' |
| 16 | + |
| 17 | +describe('TypeScript Cache Classes', () => { |
| 18 | + describe('TypeScriptMemoryCache', () => { |
| 19 | + let cache: TypeScriptMemoryCache |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + cache = new TypeScriptMemoryCache() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should create an empty cache', () => { |
| 26 | + expect(cache.memoryCache.size).toBe(0) |
| 27 | + expect(cache.path).toBe('(in-memory)') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should throw error for unknown versions', async () => { |
| 31 | + await expect(cache.load('4.0.0')).rejects.toThrow( |
| 32 | + 'TypeScript version 4.0.0 not in cache' |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should clear the cache', () => { |
| 37 | + // Add a mock module to the cache |
| 38 | + const mockModule = { version: '4.0.0' } as any |
| 39 | + cache.memoryCache.set('4.0.0', mockModule) |
| 40 | + |
| 41 | + expect(cache.memoryCache.size).toBe(1) |
| 42 | + cache.clear() |
| 43 | + expect(cache.memoryCache.size).toBe(0) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should free specific versions', () => { |
| 47 | + const mockModule = { version: '4.0.0' } as any |
| 48 | + cache.memoryCache.set('4.0.0', mockModule) |
| 49 | + |
| 50 | + expect(cache.memoryCache.has('4.0.0')).toBe(true) |
| 51 | + cache.free('4.0.0') |
| 52 | + expect(cache.memoryCache.has('4.0.0')).toBe(false) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should purge the cache', async () => { |
| 56 | + const mockModule = { version: '4.0.0' } as any |
| 57 | + cache.memoryCache.set('4.0.0', mockModule) |
| 58 | + |
| 59 | + expect(cache.memoryCache.size).toBe(1) |
| 60 | + await cache.purge() |
| 61 | + expect(cache.memoryCache.size).toBe(0) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('TypeScriptCDNCache', () => { |
| 66 | + let cache: TypeScriptCDNCache |
| 67 | + let tempDir: string |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + tempDir = mkdtempSync(resolve(tmpdir(), 'mtsv-test-')) |
| 71 | + cache = new TypeScriptCDNCache(tempDir) |
| 72 | + }) |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + // Clean up temp directory |
| 76 | + rmSync(tempDir, { recursive: true, force: true }) |
| 77 | + resetTestServer() // Reset to default handlers after each test |
| 78 | + }) |
| 79 | + |
| 80 | + it('should create cache with correct path', () => { |
| 81 | + expect(cache.path).toBe(resolve(tempDir, 'mtsv-cache')) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should load TypeScript version from CDN (mocked)', async () => { |
| 85 | + // Configure test to provide a passing TypeScript 4.0.0 version |
| 86 | + configureTestServer(testConfigs.singlePass('4.0.0')) |
| 87 | + |
| 88 | + const tsModule = await cache.load('4.0.0') |
| 89 | + |
| 90 | + expect(tsModule).toBeDefined() |
| 91 | + expect(tsModule.version).toBe('4.0.0') |
| 92 | + expect(tsModule.ScriptTarget).toBeDefined() |
| 93 | + }) |
| 94 | + |
| 95 | + it('should throw error for non-existent versions', async () => { |
| 96 | + // Configure test with only 4.0.0 available |
| 97 | + configureTestServer(testConfigs.singlePass('4.0.0')) |
| 98 | + |
| 99 | + await expect(cache.load('99.0.0')).rejects.toThrow( |
| 100 | + 'Failed to fetch TypeScript 99.0.0' |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should cache loaded modules in memory', async () => { |
| 105 | + // Configure test to provide TypeScript 4.0.0 |
| 106 | + configureTestServer(testConfigs.singlePass('4.0.0')) |
| 107 | + |
| 108 | + // Load the same version twice |
| 109 | + const tsModule1 = await cache.load('4.0.0') |
| 110 | + const tsModule2 = await cache.load('4.0.0') |
| 111 | + |
| 112 | + // Both should be valid TypeScript modules |
| 113 | + expect(tsModule1).toBeDefined() |
| 114 | + expect(tsModule2).toBeDefined() |
| 115 | + expect(tsModule1.version).toBe('4.0.0') |
| 116 | + expect(tsModule2.version).toBe('4.0.0') |
| 117 | + expect(tsModule1.ScriptTarget).toEqual(tsModule2.ScriptTarget) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should free cached modules', async () => { |
| 121 | + // Configure test to provide TypeScript 4.0.0 |
| 122 | + configureTestServer(testConfigs.singlePass('4.0.0')) |
| 123 | + |
| 124 | + const tsModule1 = await cache.load('4.0.0') |
| 125 | + cache.free('4.0.0') |
| 126 | + |
| 127 | + // Loading again should still work (from disk cache since mocked) |
| 128 | + const tsModule2 = await cache.load('4.0.0') |
| 129 | + expect(tsModule1.version).toBe('4.0.0') |
| 130 | + expect(tsModule2.version).toBe('4.0.0') |
| 131 | + }) |
| 132 | + |
| 133 | + it('should clear all cached modules', async () => { |
| 134 | + // Configure test to provide multiple TypeScript versions |
| 135 | + configureTestServer({ |
| 136 | + '4.0.0': true, |
| 137 | + '5.0.0': true |
| 138 | + }) |
| 139 | + |
| 140 | + await cache.load('4.0.0') |
| 141 | + await cache.load('5.0.0') |
| 142 | + |
| 143 | + // Clear should remove from memory but files should still exist |
| 144 | + cache.clear() |
| 145 | + |
| 146 | + // Loading again should work (from disk) |
| 147 | + const tsModule = await cache.load('4.0.0') |
| 148 | + expect(tsModule).toBeDefined() |
| 149 | + }) |
| 150 | + |
| 151 | + it('should purge cache directory', async () => { |
| 152 | + // Configure test to provide TypeScript 4.0.0 |
| 153 | + configureTestServer(testConfigs.singlePass('4.0.0')) |
| 154 | + |
| 155 | + await cache.load('4.0.0') |
| 156 | + |
| 157 | + // Purge should remove everything |
| 158 | + await cache.purge() |
| 159 | + |
| 160 | + // Loading again should fetch from CDN |
| 161 | + const tsModule = await cache.load('4.0.0') |
| 162 | + expect(tsModule).toBeDefined() |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + describe('Cache Interface Compliance', () => { |
| 167 | + const testCacheInterface = ( |
| 168 | + createCache: () => TypeScriptCacheInterface |
| 169 | + ) => { |
| 170 | + let cache: TypeScriptCacheInterface |
| 171 | + |
| 172 | + beforeEach(() => { |
| 173 | + cache = createCache() |
| 174 | + }) |
| 175 | + |
| 176 | + it('should have all required methods', () => { |
| 177 | + expect(typeof cache.load).toBe('function') |
| 178 | + expect(typeof cache.free).toBe('function') |
| 179 | + expect(typeof cache.clear).toBe('function') |
| 180 | + expect(typeof cache.purge).toBe('function') |
| 181 | + expect(typeof cache.path).toBe('string') |
| 182 | + }) |
| 183 | + } |
| 184 | + |
| 185 | + describe('TypeScriptMemoryCache interface compliance', () => { |
| 186 | + testCacheInterface(() => new TypeScriptMemoryCache()) |
| 187 | + }) |
| 188 | + |
| 189 | + describe('TypeScriptCDNCache interface compliance', () => { |
| 190 | + let tempDir: string |
| 191 | + |
| 192 | + beforeEach(() => { |
| 193 | + tempDir = mkdtempSync(resolve(tmpdir(), 'mtsv-test-')) |
| 194 | + }) |
| 195 | + |
| 196 | + afterEach(() => { |
| 197 | + rmSync(tempDir, { recursive: true, force: true }) |
| 198 | + resetTestServer() // Reset handlers after each test |
| 199 | + }) |
| 200 | + |
| 201 | + testCacheInterface(() => new TypeScriptCDNCache(tempDir)) |
| 202 | + }) |
| 203 | + }) |
| 204 | +}) |
0 commit comments