mirror of
https://github.com/misenhower/splatoon3.ink.git
synced 2026-11-18 16:58:18 -06:00
Prepare the shared updater and social code to run against R2 in a Worker while keeping the local Node runner working: - Add app/common/storage with public, private, and archive areas keyed like the R2 objects. FilesystemStorage maps them onto dist/ and storage/ for Node; BucketStorage wraps R2 bindings. Storage is configured once per process; only image existence is cached, so JSON reads are always fresh. - Move ValueCache, DataUpdater (data, calendars, archive snapshots), ImageProcessor, LocalizationProcessor, festival updaters, status generators, social translations, ImageWriter, and Buffer/Threads image hosting onto storage. Query hashes are a JSON import. - Add app/splatnet/auth.mjs: the existing bullet-token code behind auth(), so the Worker can route token requests to per-region Durable Objects. warmCaches uses auth().refresh(). - Remove the S3/R2 sync, vfs, the dual upload, the sync commands, and s3-sync-client. Local runs no longer read from or write to remote buckets. - Give every test fresh in-memory storage via app/test/setup.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
5.1 KiB
JavaScript
164 lines
5.1 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import ValueCache from '../common/ValueCache.mjs';
|
|
import SplatNet3Client from './SplatNet3Client.mjs';
|
|
|
|
vi.mock('../common/prefixedConsole.mjs', () => ({
|
|
default: () => ({ info: vi.fn(), debug: vi.fn() }),
|
|
}));
|
|
|
|
describe('SplatNet3Client bullet tokens', () => {
|
|
let cache;
|
|
let fetch;
|
|
|
|
function makeClient(account = 'US', language = 'en-US') {
|
|
const client = new SplatNet3Client({
|
|
region: account,
|
|
cachePrefix: `test.${account}`,
|
|
getWebServiceToken: vi.fn().mockResolvedValue(`web-token-${account}`),
|
|
}, language);
|
|
|
|
client.queryHashes = { version: 'test-version' };
|
|
|
|
return client;
|
|
}
|
|
|
|
function tokenRequests() {
|
|
return fetch.mock.calls.filter(([url]) => url.endsWith('/api/bullet_tokens'));
|
|
}
|
|
|
|
beforeEach(() => {
|
|
cache = new Map();
|
|
|
|
vi.spyOn(ValueCache.prototype, 'getData').mockImplementation(async function () {
|
|
return cache.get(this.path) ?? null;
|
|
});
|
|
|
|
vi.spyOn(ValueCache.prototype, 'setData').mockImplementation(async function (data) {
|
|
cache.set(this.path, data);
|
|
});
|
|
|
|
fetch = vi.fn(async (url, options) => ({
|
|
ok: true,
|
|
json: async () => url.endsWith('/api/bullet_tokens')
|
|
? { bulletToken: `bullet-${options.headers['X-GameWebToken']}` }
|
|
: { data: {} },
|
|
}));
|
|
|
|
vi.stubGlobal('fetch', fetch);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('reuses one token across simultaneous updater clients and locales', async () => {
|
|
const clients = Array.from({ length: 5 }, (_, index) => (
|
|
makeClient('US', index === 0 ? 'en-US' : 'es-MX')
|
|
));
|
|
|
|
await Promise.all(clients.map(client => client.getStageScheduleData()));
|
|
|
|
expect(tokenRequests()).toHaveLength(1);
|
|
expect(fetch.mock.calls.filter(([url]) => url.endsWith('/api/graphql'))).toHaveLength(5);
|
|
expect(clients.map(client => client.bulletToken)).toEqual(
|
|
Array(5).fill({ bulletToken: 'bullet-web-token-US' }),
|
|
);
|
|
});
|
|
|
|
it('reuses one token across concurrent queries on one client', async () => {
|
|
const client = makeClient();
|
|
|
|
await Promise.all([client.getStageScheduleData(), client.getGesotownData()]);
|
|
|
|
expect(tokenRequests()).toHaveLength(1);
|
|
});
|
|
|
|
it('allows separate accounts to acquire tokens independently', async () => {
|
|
const gate = Promise.withResolvers();
|
|
fetch.mockImplementationOnce(() => gate.promise);
|
|
|
|
const usToken = makeClient('US').getBulletToken();
|
|
await vi.waitFor(() => expect(tokenRequests()).toHaveLength(1));
|
|
|
|
try {
|
|
const euToken = await makeClient('EU').getBulletToken();
|
|
|
|
expect(euToken.bulletToken).toBe('bullet-web-token-EU');
|
|
expect(tokenRequests()).toHaveLength(2);
|
|
} finally {
|
|
gate.resolve({ ok: true, json: async () => ({ bulletToken: 'us-token' }) });
|
|
await usToken;
|
|
}
|
|
});
|
|
|
|
it('reuses the cache and acquires again after the cache becomes unavailable', async () => {
|
|
await makeClient().getBulletToken();
|
|
await makeClient().getBulletToken();
|
|
|
|
expect(tokenRequests()).toHaveLength(1);
|
|
|
|
cache.clear();
|
|
await makeClient().getBulletToken();
|
|
|
|
expect(tokenRequests()).toHaveLength(2);
|
|
});
|
|
|
|
it('allows the next queued caller to retry after a failure', async () => {
|
|
fetch.mockResolvedValueOnce({ ok: false, status: 503 });
|
|
|
|
const results = await Promise.allSettled([
|
|
makeClient().getBulletToken(),
|
|
makeClient().getBulletToken(),
|
|
]);
|
|
|
|
expect(results.map(result => result.status)).toEqual(['rejected', 'fulfilled']);
|
|
expect(results[0].reason.message).toBe('Invalid bullet token response code: 503');
|
|
expect(results[1].value).toEqual({ bulletToken: 'bullet-web-token-US' });
|
|
expect(tokenRequests()).toHaveLength(2);
|
|
|
|
await expect(makeClient().getBulletToken()).resolves.toEqual({
|
|
bulletToken: 'bullet-web-token-US',
|
|
});
|
|
|
|
expect(tokenRequests()).toHaveLength(2);
|
|
});
|
|
|
|
it('serializes forced refreshes while bypassing a cached token', async () => {
|
|
cache.set('cache/test.US.bulletToken.json', { bulletToken: 'old-token' });
|
|
|
|
let activeRequests = 0;
|
|
let maxActiveRequests = 0;
|
|
|
|
fetch.mockImplementation(async () => {
|
|
activeRequests++;
|
|
maxActiveRequests = Math.max(maxActiveRequests, activeRequests);
|
|
|
|
await Promise.resolve();
|
|
activeRequests--;
|
|
|
|
return { ok: true, json: async () => ({ bulletToken: 'fresh-token' }) };
|
|
});
|
|
|
|
const tokens = await Promise.all([
|
|
makeClient().getBulletToken(false),
|
|
makeClient().getBulletToken(false),
|
|
]);
|
|
|
|
expect(tokens).toEqual(Array(2).fill({ bulletToken: 'fresh-token' }));
|
|
expect(tokenRequests()).toHaveLength(2);
|
|
expect(maxActiveRequests).toBe(1);
|
|
});
|
|
|
|
it('does not let an in-progress cache lookup suppress a forced refresh', async () => {
|
|
cache.set('cache/test.US.bulletToken.json', { bulletToken: 'old-token' });
|
|
|
|
const cached = makeClient().getBulletToken();
|
|
const refreshed = makeClient().getBulletToken(false);
|
|
|
|
expect(await cached).toEqual({ bulletToken: 'old-token' });
|
|
expect(await refreshed).toEqual({ bulletToken: 'bullet-web-token-US' });
|
|
expect(tokenRequests()).toHaveLength(1);
|
|
});
|
|
});
|