freshyo/apps/user-ui/tests/lib/image-cache-ttl.test.tsx
2026-11-01 19:11:48 +05:30

98 lines
3.4 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* IMGCACHE-01..07 — the 3-day TTL for expo-image's disk cache.
*
* Helper boundaries (01..04) and the mounted component (05..07). The timestamp
* file helper is mocked here; its own file-system plumbing is covered in
* `image-cache-meta.test.ts`.
*/
import * as React from 'react'
import { render, waitFor } from '@testing-library/react-native'
import { Image } from 'expo-image'
import ImageCacheTtl from '@/src/components/ImageCacheTtl'
import { IMAGE_CACHE_TTL_MS, shouldClearImageCache } from '@/src/lib/image-cache-ttl'
import {
readImageCacheClearedAt,
writeImageCacheClearedAt,
} from '@/src/lib/image-cache-meta'
import { resetAllMocks } from '../helpers/reset'
jest.mock('@/src/lib/image-cache-meta', () => ({
readImageCacheClearedAt: jest.fn(),
writeImageCacheClearedAt: jest.fn(async () => undefined),
}))
const readClearedAt = readImageCacheClearedAt as unknown as jest.Mock
const writeClearedAt = writeImageCacheClearedAt as unknown as jest.Mock
const clearDiskCache = Image.clearDiskCache as unknown as jest.Mock
describe('image cache TTL', () => {
beforeEach(() => {
resetAllMocks()
readClearedAt.mockReset()
writeClearedAt.mockReset()
writeClearedAt.mockResolvedValue(undefined)
})
it('IMGCACHE-01: a missing, zero or invalid timestamp clears', () => {
expect(shouldClearImageCache(null)).toBe(true)
expect(shouldClearImageCache(0)).toBe(true)
expect(shouldClearImageCache(Number.NaN)).toBe(true)
})
it('IMGCACHE-02: a timestamp younger than three days keeps the cache', () => {
const now = Date.now()
expect(shouldClearImageCache(now - IMAGE_CACHE_TTL_MS + 1, now)).toBe(false)
expect(shouldClearImageCache(now - 1000, now)).toBe(false)
})
it('IMGCACHE-03: exactly three days (or older) clears', () => {
const now = Date.now()
expect(shouldClearImageCache(now - IMAGE_CACHE_TTL_MS, now)).toBe(true)
expect(shouldClearImageCache(now - IMAGE_CACHE_TTL_MS - 1000, now)).toBe(true)
})
it('IMGCACHE-04: a future timestamp (clock skew) clears', () => {
const now = Date.now()
expect(shouldClearImageCache(now + 60_000, now)).toBe(true)
})
it('IMGCACHE-05: an expired cache is cleared and the timestamp is refreshed', async () => {
readClearedAt.mockResolvedValue(Date.now() - IMAGE_CACHE_TTL_MS - 1000)
const view = render(<ImageCacheTtl />)
await waitFor(() => expect(clearDiskCache).toHaveBeenCalledTimes(1))
await waitFor(() => expect(writeClearedAt).toHaveBeenCalledTimes(1))
expect(writeClearedAt.mock.calls[0][0]).toBeGreaterThan(Date.now() - 5000)
view.unmount()
})
it('IMGCACHE-06: a fresh cache is left alone', async () => {
readClearedAt.mockResolvedValue(Date.now() - 1000)
const view = render(<ImageCacheTtl />)
await waitFor(() => expect(readClearedAt).toHaveBeenCalled())
await new Promise((resolve) => setTimeout(resolve, 20))
expect(clearDiskCache).not.toHaveBeenCalled()
expect(writeClearedAt).not.toHaveBeenCalled()
view.unmount()
})
it('IMGCACHE-07: a failed clear does not persist a timestamp (retries next time)', async () => {
readClearedAt.mockResolvedValue(null)
clearDiskCache.mockResolvedValueOnce(false)
const view = render(<ImageCacheTtl />)
await waitFor(() => expect(clearDiskCache).toHaveBeenCalledTimes(1))
expect(writeClearedAt).not.toHaveBeenCalled()
view.unmount()
})
})