Files
splatoon3.ink/app/splatnet/NsoClient.test.mjs
Matt Isenhower 8a02e2bfdc Make the shared backend code run in Cloudflare Workers
Replace the remaining Node-only dependencies so the updaters and social
posters bundle and run in workerd, while the Node runner keeps working:

- Replace nxapi with app/splatnet/coral.mjs (fetch + WebCrypto), aligned
  with nxapi 1.6.1-next.257: znca compatibility 3.5.0, the current client
  version, and the slimmer Login parameter. Use one znca-api client per
  Nintendo account, since its token binds to the first account it serves.
  Cache the remote config in private storage instead of restarting daily.
  Sessions cached by the nxapi package trigger a fresh login.
- Convert images through #image-converter: sharp in Node, the Images
  binding in Workers. Read Bluesky aspect ratios from the PNG header.
- Replace twitter-api-v2 with OAuth 1.0a signed fetch; TwitterClient now
  only updates the seasonal X profile (posts go through Buffer).
- Load avatars through #avatars (bundled as Data modules in Workers) and
  keep the local Chrome driver out of Worker bundles via
  #local-screenshot-driver.
- prefixedConsole forwards to the global console; shared code reports to
  Sentry through @sentry/core.

Verified live: full credential refresh for all four regions in Node, and
the complete Coral → bullet token → GraphQL chain for EU inside workerd.

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

126 lines
4.7 KiB
JavaScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { testBuckets } from '../test/storage.mjs';
const coral = vi.hoisted(() => ({
getRemoteConfig: vi.fn(),
loginWithSessionToken: vi.fn(),
getWebServiceToken: vi.fn(),
ZncaApiClient: vi.fn(function (options) {
Object.assign(this, options);
}),
}));
vi.mock('./coral.mjs', () => coral);
vi.mock('../common/prefixedConsole.mjs', () => ({
default: () => ({ info: vi.fn(), debug: vi.fn() }),
}));
const { default: NsoClient, getZnca } = await import('./NsoClient.mjs');
const session = {
user: { id: 'na-user' },
coralUserId: 1234,
credential: { accessToken: 'coral-token', expiresIn: 7200 },
};
describe('NsoClient', () => {
beforeEach(() => {
vi.stubEnv('NINTENDO_TOKEN_US', 'session-token-us-12345678');
vi.stubEnv('NXAPI_CLIENT_ID', 'client');
vi.stubEnv('NXAPI_SHARED_SECRET', 'secret');
vi.stubEnv('USER_AGENT', 'test-agent');
coral.getRemoteConfig.mockResolvedValue({
zncaVersion: '3.4.0',
zncaApiUrl: 'https://znca.test/api/',
cacheControl: 'public, max-age=1800',
});
coral.loginWithSessionToken.mockResolvedValue(session);
coral.getWebServiceToken.mockResolvedValue({ accessToken: 'web-token', expiresIn: 7200 });
});
afterEach(() => {
vi.unstubAllEnvs();
vi.clearAllMocks();
});
it('caches the nxapi remote config in private storage', async () => {
let first = await getZnca('nso.a');
let second = await getZnca('nso.a');
let otherAccount = await getZnca('nso.b');
expect(coral.getRemoteConfig).toHaveBeenCalledOnce();
expect(coral.getRemoteConfig).toHaveBeenCalledWith('splatoon3.ink test-agent');
expect(first.zncaVersion).toBe('3.4.0');
expect(second.znca).toBe(first.znca);
// znca-api tokens become bound to one Nintendo account
expect(otherAccount.znca).not.toBe(first.znca);
let cached = testBuckets().private.json('cache/nxapi.config.json');
expect(cached.data.zncaApiUrl).toBe('https://znca.test/api/');
expect(cached.expires - Date.now()).toBeGreaterThan(29 * 60 * 1000);
expect(cached.expires - Date.now()).toBeLessThanOrEqual(30 * 60 * 1000);
});
it('does not cache a remote config that disables Coral authentication', async () => {
coral.getRemoteConfig.mockRejectedValue(new Error('Remote configuration prevents Coral authentication'));
await expect(getZnca('nso.a')).rejects.toThrow('prevents Coral authentication');
expect(testBuckets().private.objects.size).toBe(0);
});
it('requires nxapi client credentials', async () => {
vi.stubEnv('NXAPI_SHARED_SECRET', '');
await expect(getZnca('nso.a')).rejects.toThrow('NXAPI_CLIENT_ID and NXAPI_SHARED_SECRET');
});
it('logs in once and reuses the cached Coral session', async () => {
let nso = NsoClient.make('US');
expect(await nso.getCoralSession()).toEqual(session);
expect(await nso.getCoralSession()).toEqual(session);
expect(coral.loginWithSessionToken).toHaveBeenCalledOnce();
expect(coral.loginWithSessionToken.mock.calls[0][0]).toBe('session-token-us-12345678');
expect(testBuckets().private.json('cache/nso.12345678.coral.json').data).toEqual(session);
});
it('logs in again instead of using a session cached by the nxapi package', async () => {
await testBuckets().private.put('cache/nso.12345678.coral.json', JSON.stringify({
expires: Date.now() + 3600_000,
data: { nintendoAccountToken: {}, nsoAccount: {}, credential: { accessToken: 'old' } },
}));
expect(await NsoClient.make('US').getCoralSession()).toEqual(session);
expect(coral.loginWithSessionToken).toHaveBeenCalledOnce();
});
it('creates and caches web service tokens from the Coral session', async () => {
let nso = NsoClient.make('US');
expect(await nso.getWebServiceToken('4834290508791808')).toBe('web-token');
expect(await nso.getWebServiceToken('4834290508791808')).toBe('web-token');
expect(coral.getWebServiceToken).toHaveBeenCalledOnce();
expect(coral.getWebServiceToken.mock.calls[0]).toEqual(['4834290508791808', expect.objectContaining({
zncaVersion: '3.4.0',
coralAccessToken: 'coral-token',
coralUserId: 1234,
naId: 'na-user',
})]);
expect(testBuckets().private.json('cache/nso.12345678.webservicetoken.4834290508791808.json').data)
.toEqual({ accessToken: 'web-token', expiresIn: 7200 });
});
it('bypasses the caches when asked', async () => {
let nso = NsoClient.make('US');
await nso.getWebServiceToken('1');
await nso.getCoralSession(false);
await nso.getWebServiceToken('1', false);
expect(coral.loginWithSessionToken).toHaveBeenCalledTimes(2);
expect(coral.getWebServiceToken).toHaveBeenCalledTimes(2);
});
});