|
1 | | -import { existsSync, readFileSync, statSync, utimesSync, writeFileSync } from 'node:fs'; |
| 1 | +import { readFileSync, statSync, utimesSync, writeFileSync } from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
3 | 3 | import { expect, test } from 'rstack/test'; |
4 | 4 | import { cacheNamespace, createOptionsHasher, sha256 } from '../../src/fmt/cacheIdentity.ts'; |
@@ -160,17 +160,67 @@ test('does not cache formatting errors', async () => { |
160 | 160 | }); |
161 | 161 | }); |
162 | 162 |
|
163 | | -test('does not apply the cache in write mode yet', async () => { |
| 163 | +test('write persists clean results for misses and hits', async () => { |
| 164 | + await withTempProject(async (rootPath) => { |
| 165 | + const cleanPath = path.join(rootPath, 'clean.ts'); |
| 166 | + const dirtyPath = path.join(rootPath, 'dirty.ts'); |
| 167 | + const cache = createCache(rootPath); |
| 168 | + writeFileSync(cleanPath, 'const clean = 1;\n'); |
| 169 | + writeFileSync(dirtyPath, 'const dirty=1'); |
| 170 | + |
| 171 | + const files = [createRequest(cleanPath), createRequest(dirtyPath)]; |
| 172 | + await expect(run(files, 'write', cache)).resolves.toMatchObject({ |
| 173 | + exitCode: 0, |
| 174 | + files: [{ path: dirtyPath, status: 'written' }], |
| 175 | + processedFileCount: 2, |
| 176 | + }); |
| 177 | + |
| 178 | + const store = await loadFmtCacheStore(cache.filePath, cacheNamespace); |
| 179 | + expect(store.get('clean.ts')).toEqual([ |
| 180 | + sha256(readFileSync(cleanPath)), |
| 181 | + expect.any(String), |
| 182 | + 'clean', |
| 183 | + ]); |
| 184 | + expect(store.get('dirty.ts')).toEqual([ |
| 185 | + sha256(readFileSync(dirtyPath)), |
| 186 | + expect.any(String), |
| 187 | + 'clean', |
| 188 | + ]); |
| 189 | + |
| 190 | + const timestamps = files.map((file) => statSync(file.path).mtimeMs); |
| 191 | + await expect(run(files, 'write', cache)).resolves.toMatchObject({ |
| 192 | + exitCode: 0, |
| 193 | + files: [], |
| 194 | + processedFileCount: 2, |
| 195 | + }); |
| 196 | + expect(files.map((file) => statSync(file.path).mtimeMs)).toEqual(timestamps); |
| 197 | + }); |
| 198 | +}); |
| 199 | + |
| 200 | +test('write converts a dirty entry to clean', async () => { |
164 | 201 | await withTempProject(async (rootPath) => { |
165 | 202 | const filePath = path.join(rootPath, 'index.ts'); |
166 | 203 | const cache = createCache(rootPath); |
| 204 | + const file = createRequest(filePath); |
167 | 205 | writeFileSync(filePath, 'const value=1'); |
168 | 206 |
|
169 | | - await expect(run([createRequest(filePath)], 'write', cache)).resolves.toMatchObject({ |
| 207 | + await run([file], 'check', cache); |
| 208 | + |
| 209 | + await expect(run([file], 'write', cache)).resolves.toMatchObject({ |
170 | 210 | exitCode: 0, |
171 | 211 | files: [{ path: filePath, status: 'written' }], |
172 | 212 | }); |
173 | 213 | expect(readFileSync(filePath, 'utf8')).toBe('const value = 1;\n'); |
174 | | - expect(existsSync(cache.filePath)).toBe(false); |
| 214 | + |
| 215 | + const store = await loadFmtCacheStore(cache.filePath, cacheNamespace); |
| 216 | + expect(store.get('index.ts')).toEqual([ |
| 217 | + sha256(readFileSync(filePath)), |
| 218 | + expect.any(String), |
| 219 | + 'clean', |
| 220 | + ]); |
| 221 | + await expect(run([file], 'check', cache)).resolves.toMatchObject({ |
| 222 | + exitCode: 0, |
| 223 | + files: [], |
| 224 | + }); |
175 | 225 | }); |
176 | 226 | }); |
0 commit comments