Skip to content

Commit 89f551e

Browse files
committed
chore: fix hot-update
1 parent f107266 commit 89f551e

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

packages/weapp-tailwindcss/test/cache/hot-update.test.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Cache Hot Update', () => {
1818

1919
describe('processCachedTask', () => {
2020
it('should call transform on cache miss', async () => {
21-
const cache = initializeCache({})
21+
const cache = initializeCache()
2222
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
2323
const applyResult = vi.fn()
2424

@@ -35,7 +35,7 @@ describe('Cache Hot Update', () => {
3535
})
3636

3737
it('should hit cache on second call with same key and source', async () => {
38-
const cache = initializeCache({})
38+
const cache = initializeCache()
3939
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
4040
const applyResult = vi.fn()
4141
const onCacheHit = vi.fn()
@@ -72,7 +72,7 @@ describe('Cache Hot Update', () => {
7272
})
7373

7474
it('should invalidate cache when source changes', async () => {
75-
const cache = initializeCache({})
75+
const cache = initializeCache()
7676
const transform = vi.fn()
7777
.mockResolvedValueOnce({ result: 'transformed-1' })
7878
.mockResolvedValueOnce({ result: 'transformed-2' })
@@ -106,7 +106,7 @@ describe('Cache Hot Update', () => {
106106
})
107107

108108
it('should use different cache for different keys', async () => {
109-
const cache = initializeCache({})
109+
const cache = initializeCache()
110110
const transform1 = vi.fn().mockResolvedValue({ result: 'result-1' })
111111
const transform2 = vi.fn().mockResolvedValue({ result: 'result-2' })
112112
const applyResult1 = vi.fn()
@@ -135,7 +135,7 @@ describe('Cache Hot Update', () => {
135135
})
136136

137137
it('should handle custom hashKey', async () => {
138-
const cache = initializeCache({})
138+
const cache = initializeCache()
139139
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
140140
const applyResult = vi.fn()
141141
const onCacheHit = vi.fn()
@@ -170,13 +170,28 @@ describe('Cache Hot Update', () => {
170170
})
171171

172172
it('should handle readCache function', async () => {
173-
const cache = initializeCache({})
173+
const cache = initializeCache()
174174
const cachedValue = 'from-custom-cache'
175175
const readCache = vi.fn().mockReturnValue(cachedValue)
176176
const applyResult = vi.fn()
177-
const transform = vi.fn()
177+
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
178178
const onCacheHit = vi.fn()
179179

180+
// 第一次调用,缓存未命中
181+
await processCachedTask({
182+
cache,
183+
cacheKey: 'test-key',
184+
rawSource: 'source',
185+
applyResult,
186+
transform,
187+
})
188+
189+
expect(transform).toHaveBeenCalledTimes(1)
190+
expect(readCache).not.toHaveBeenCalled()
191+
192+
applyResult.mockClear()
193+
194+
// 第二次调用,使用自定义 readCache
180195
await processCachedTask({
181196
cache,
182197
cacheKey: 'test-key',
@@ -190,11 +205,11 @@ describe('Cache Hot Update', () => {
190205
expect(readCache).toHaveBeenCalled()
191206
expect(onCacheHit).toHaveBeenCalled()
192207
expect(applyResult).toHaveBeenCalledWith(cachedValue)
193-
expect(transform).not.toHaveBeenCalled()
208+
expect(transform).toHaveBeenCalledTimes(1) // 不应该再次调用 transform
194209
})
195210

196211
it('should handle applyResult that returns promise', async () => {
197-
const cache = initializeCache({})
212+
const cache = initializeCache()
198213
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
199214
const applyResult = vi.fn().mockResolvedValue(undefined)
200215

@@ -210,7 +225,7 @@ describe('Cache Hot Update', () => {
210225
})
211226

212227
it('BC-003: should handle cache key conflicts with MD5 hash', async () => {
213-
const cache = initializeCache({})
228+
const cache = initializeCache()
214229
const transform = vi.fn()
215230
.mockResolvedValueOnce({ result: 'result-1' })
216231
.mockResolvedValueOnce({ result: 'result-2' })
@@ -244,7 +259,7 @@ describe('Cache Hot Update', () => {
244259

245260
describe('Cache boundary conditions', () => {
246261
it('BC-009: should handle empty source', async () => {
247-
const cache = initializeCache({})
262+
const cache = initializeCache()
248263
const transform = vi.fn().mockResolvedValue({ result: '' })
249264
const applyResult = vi.fn()
250265

@@ -261,7 +276,7 @@ describe('Cache Hot Update', () => {
261276
})
262277

263278
it('should handle large source content', async () => {
264-
const cache = initializeCache({})
279+
const cache = initializeCache()
265280
const largeSource = 'x'.repeat(10 * 1024 * 1024) // 10MB
266281
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
267282
const applyResult = vi.fn()
@@ -279,7 +294,7 @@ describe('Cache Hot Update', () => {
279294
})
280295

281296
it('should handle special characters in cache key', async () => {
282-
const cache = initializeCache({})
297+
const cache = initializeCache()
283298
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
284299
const applyResult = vi.fn()
285300

@@ -307,7 +322,7 @@ describe('Cache Hot Update', () => {
307322
})
308323

309324
it('should handle transform errors', async () => {
310-
const cache = initializeCache({})
325+
const cache = initializeCache()
311326
const transform = vi.fn().mockRejectedValue(new Error('Transform failed'))
312327
const applyResult = vi.fn()
313328

@@ -324,7 +339,7 @@ describe('Cache Hot Update', () => {
324339
})
325340

326341
it('should handle undefined rawSource', async () => {
327-
const cache = initializeCache({})
342+
const cache = initializeCache()
328343
const transform = vi.fn().mockResolvedValue({ result: 'transformed' })
329344
const applyResult = vi.fn()
330345

@@ -343,7 +358,7 @@ describe('Cache Hot Update', () => {
343358

344359
describe('Cache invalidation strategies', () => {
345360
it('should support manual cache invalidation', async () => {
346-
const cache = initializeCache({})
361+
const cache = initializeCache()
347362
const transform = vi.fn()
348363
.mockResolvedValueOnce({ result: 'result-1' })
349364
.mockResolvedValueOnce({ result: 'result-2' })
@@ -382,7 +397,7 @@ describe('Cache Hot Update', () => {
382397

383398
it('BC-004: should invalidate cache on version change (simulated)', async () => {
384399
// 模拟版本变化导致缓存失效
385-
const cache1 = initializeCache({})
400+
const cache1 = initializeCache()
386401
const transform = vi.fn().mockResolvedValue({ result: 'result-1' })
387402
const applyResult = vi.fn()
388403

@@ -397,7 +412,7 @@ describe('Cache Hot Update', () => {
397412
expect(transform).toHaveBeenCalledTimes(1)
398413

399414
// 模拟版本升级后,创建新的 cache 实例
400-
const cache2 = initializeCache({})
415+
const cache2 = initializeCache()
401416
transform.mockResolvedValue({ result: 'result-2' })
402417
applyResult.mockClear()
403418

@@ -417,7 +432,7 @@ describe('Cache Hot Update', () => {
417432

418433
describe('Concurrent cache operations', () => {
419434
it('BC-005: should handle concurrent requests for same key', async () => {
420-
const cache = initializeCache({})
435+
const cache = initializeCache()
421436
let transformCallCount = 0
422437
const transform = vi.fn(async () => {
423438
transformCallCount++

0 commit comments

Comments
 (0)