58 lines
2.1 KiB
TypeScript
58 lines
2.1 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* IMGCACHE-08..10 — the plain-file timestamp used by the 3-day image cache TTL.
|
|
*
|
|
* expo-file-system is mocked; this pins the read/write contract and the
|
|
* fallbacks (missing file, corrupt content) without touching native storage.
|
|
*/
|
|
import * as FileSystem from 'expo-file-system'
|
|
import {
|
|
readImageCacheClearedAt,
|
|
writeImageCacheClearedAt,
|
|
} from '@/src/lib/image-cache-meta'
|
|
import { IMAGE_CACHE_CLEARED_AT_KEY } from '@/src/lib/image-cache-ttl'
|
|
|
|
jest.mock('expo-file-system', () => ({
|
|
documentDirectory: 'file:///documents/',
|
|
getInfoAsync: jest.fn(),
|
|
readAsStringAsync: jest.fn(),
|
|
writeAsStringAsync: jest.fn(async () => undefined),
|
|
}))
|
|
|
|
const getInfoAsync = FileSystem.getInfoAsync as unknown as jest.Mock
|
|
const readAsStringAsync = FileSystem.readAsStringAsync as unknown as jest.Mock
|
|
const writeAsStringAsync = FileSystem.writeAsStringAsync as unknown as jest.Mock
|
|
|
|
const FILE_URI = `file:///documents/${IMAGE_CACHE_CLEARED_AT_KEY}`
|
|
|
|
describe('image cache timestamp file', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
it('IMGCACHE-08: a missing file reads as null', async () => {
|
|
getInfoAsync.mockResolvedValue({ exists: false })
|
|
|
|
await expect(readImageCacheClearedAt()).resolves.toBeNull()
|
|
expect(readAsStringAsync).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('IMGCACHE-09: valid content parses and corrupt content reads as null', async () => {
|
|
getInfoAsync.mockResolvedValue({ exists: true })
|
|
|
|
readAsStringAsync.mockResolvedValueOnce('1700000000000')
|
|
await expect(readImageCacheClearedAt()).resolves.toBe(1700000000000)
|
|
|
|
readAsStringAsync.mockResolvedValueOnce('not-a-number')
|
|
await expect(readImageCacheClearedAt()).resolves.toBeNull()
|
|
|
|
readAsStringAsync.mockRejectedValueOnce(new Error('io'))
|
|
await expect(readImageCacheClearedAt()).resolves.toBeNull()
|
|
})
|
|
|
|
it('IMGCACHE-10: writing stores the timestamp under the document directory', async () => {
|
|
await writeImageCacheClearedAt(1700000000123)
|
|
|
|
expect(writeAsStringAsync).toHaveBeenCalledWith(FILE_URI, '1700000000123')
|
|
})
|
|
})
|