Files
splatoon3.ink/app/common/ValueCache.test.mjs
Matt Isenhower 45f50c917b
Some checks are pending
Build frontend / build (24.x) (push) Waiting to run
Lint / lint (push) Waiting to run
Tests / test (24.x) (push) Waiting to run
Don't repost when the last post time can't be read
During an R2 outage, failed reads of the last post time looked like
"never posted", so every account posted again. Read it strictly and skip
an account whose time can't be read. Retry saving the time after a post,
and record a post that went out separately from a failed save.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:16:29 -08:00

111 lines
3.7 KiB
JavaScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import ValueCache from './ValueCache.mjs';
import { testBuckets } from '../test/storage.mjs';
import { storage } from './storage/index.mjs';
describe('ValueCache', () => {
let cache;
function store(item) {
return testBuckets().private.put('cache/test-key.json', JSON.stringify(item));
}
beforeEach(() => {
cache = new ValueCache('test-key');
});
describe('path', () => {
it('returns the private storage key cache/{key}.json', () => {
expect(cache.path).toBe('cache/test-key.json');
expect(new ValueCache('other').path).toBe('cache/other.json');
});
});
describe('get', () => {
it('returns the item when valid and not expired', async () => {
const item = { data: 'hello', expires: Date.now() + 60000, cachedAt: new Date().toISOString() };
await store(item);
expect(await cache.get()).toEqual(item);
});
it('returns null when expired', async () => {
await store({ data: 'hello', expires: Date.now() - 60000, cachedAt: new Date().toISOString() });
expect(await cache.get()).toBeNull();
});
it('treats an expiry stored as a date string the same way', async () => {
await store({ data: 'hello', expires: new Date(Date.now() - 60000).toISOString() });
expect(await cache.get()).toBeNull();
});
it('returns item when no expiry is set', async () => {
const item = { data: 'hello', expires: null, cachedAt: new Date().toISOString() };
await store(item);
expect(await cache.get()).toEqual(item);
});
it('returns null when missing or unreadable', async () => {
expect(await cache.get()).toBeNull();
await testBuckets().private.put('cache/test-key.json', 'not json');
expect(await cache.get()).toBeNull();
});
});
describe('getData', () => {
it('returns .data field from cached item', async () => {
await store({ data: { foo: 'bar' }, expires: Date.now() + 60000 });
expect(await cache.getData()).toEqual({ foo: 'bar' });
});
it('returns null when no cache', async () => {
expect(await cache.getData()).toBeNull();
expect(await cache.getData({ strict: true })).toBeNull();
});
it('throws a failed read only when strict', async () => {
const read = vi.spyOn(storage().private, 'readJson').mockRejectedValue(new Error('internal error'));
try {
expect(await cache.getData()).toBeNull();
await expect(cache.getData({ strict: true })).rejects.toThrow('internal error');
} finally {
read.mockRestore();
}
});
});
describe('getCachedAt', () => {
it('returns Date from cachedAt field, even when expired', async () => {
const cachedAt = '2024-06-15T10:00:00.000Z';
await store({ data: 'x', expires: 1, cachedAt });
expect(await cache.getCachedAt()).toEqual(new Date(cachedAt));
});
it('returns null when no data', async () => {
expect(await cache.getCachedAt()).toBeNull();
});
});
describe('setData', () => {
it('writes readable JSON with data, expires, and cachedAt', async () => {
const expires = Date.now() + 60000;
await cache.setData({ key: 'value' }, expires);
expect(testBuckets().private.text('cache/test-key.json')).toContain('"key": "value"');
const written = testBuckets().private.json('cache/test-key.json');
expect(written.data).toEqual({ key: 'value' });
expect(written.expires).toBe(expires);
expect(written.cachedAt).toBeDefined();
expect(await cache.getData()).toEqual({ key: 'value' });
});
it('stores null expires when not specified', async () => {
await cache.setData('test');
expect(testBuckets().private.json('cache/test-key.json').expires).toBeNull();
});
});
});