mirror of
https://github.com/misenhower/splatoon3.ink.git
synced 2026-11-18 16:58:18 -06:00
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>
122 lines
4.0 KiB
JavaScript
122 lines
4.0 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import fs from 'fs/promises';
|
|
import ValueCache from '../common/ValueCache.mjs';
|
|
import BlueskyClient from './clients/BlueskyClient.mjs';
|
|
import BufferTwitterClient from './clients/BufferTwitterClient.mjs';
|
|
import TwitterClient from './clients/TwitterClient.mjs';
|
|
import { defaultClients } from './index.mjs';
|
|
import { updateAvatars } from './updateAvatars.mjs';
|
|
|
|
vi.mock('../common/prefixedConsole.mjs', () => ({
|
|
default: () => ({ log: vi.fn(), error: vi.fn() }),
|
|
}));
|
|
|
|
const twitterCredentials = [
|
|
'TWITTER_CONSUMER_KEY',
|
|
'TWITTER_CONSUMER_SECRET',
|
|
'TWITTER_ACCESS_TOKEN_KEY',
|
|
'TWITTER_ACCESS_TOKEN_SECRET',
|
|
];
|
|
|
|
describe('Seasonal profile updates', () => {
|
|
let cache;
|
|
let updateImage;
|
|
let updateProfile;
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date(2026, 9, 17, 12));
|
|
|
|
for (let key of twitterCredentials) {
|
|
vi.stubEnv(key, 'test-credential');
|
|
}
|
|
|
|
vi.stubEnv('MASTODON_ACCESS_TOKEN', '');
|
|
vi.stubEnv('BLUESKY_PASSWORD', '');
|
|
vi.stubEnv('BUFFER_API_KEY', '');
|
|
vi.stubEnv('BUFFER_TWITTER_CHANNEL_ID', '');
|
|
vi.stubEnv('BLUESKY_JP_PASSWORD', '');
|
|
|
|
cache = new Map;
|
|
|
|
vi.spyOn(ValueCache.prototype, 'getData').mockImplementation(async function () {
|
|
return cache.get(this._key);
|
|
});
|
|
|
|
vi.spyOn(ValueCache.prototype, 'setData').mockImplementation(async function (value) {
|
|
cache.set(this._key, value);
|
|
});
|
|
|
|
updateImage = vi.fn().mockResolvedValue({});
|
|
updateProfile = vi.fn().mockResolvedValue({});
|
|
|
|
vi.spyOn(TwitterClient.prototype, 'request').mockImplementation(async (path, { params, body }) => {
|
|
return path === 'account/update_profile_image.json'
|
|
? updateImage(new Uint8Array(await body.get('image').arrayBuffer()))
|
|
: updateProfile(params);
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllEnvs();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('keeps Twitter posts on Buffer', () => {
|
|
let clients = defaultClients();
|
|
|
|
expect(clients.some(client => client instanceof BufferTwitterClient)).toBe(true);
|
|
expect(clients.some(client => client instanceof TwitterClient)).toBe(false);
|
|
});
|
|
|
|
it.each(twitterCredentials)('skips Twitter when %s is missing', async key => {
|
|
vi.stubEnv(key, '');
|
|
|
|
await updateAvatars();
|
|
|
|
expect(updateImage).not.toHaveBeenCalled();
|
|
expect(updateProfile).not.toHaveBeenCalled();
|
|
expect(cache.size).toBe(0);
|
|
});
|
|
|
|
it('updates the seasonal profile without Buffer credentials and skips it on the next run', async () => {
|
|
let avatar = await fs.readFile(new URL('./avatars/halloween.png', import.meta.url));
|
|
|
|
await updateAvatars();
|
|
await updateAvatars();
|
|
|
|
expect(updateImage).toHaveBeenCalledExactlyOnceWith(new Uint8Array(avatar));
|
|
expect(updateProfile).toHaveBeenCalledExactlyOnceWith({ name: 'Splatoon3.eek 🎃' });
|
|
expect(cache.get('social.twitter.avatar')).toBe('halloween');
|
|
});
|
|
|
|
it('retries on the next run when only the image update succeeds', async () => {
|
|
updateProfile.mockRejectedValueOnce(new Error('X unavailable'));
|
|
|
|
await updateAvatars();
|
|
|
|
expect(cache.has('social.twitter.avatar')).toBe(false);
|
|
|
|
await updateAvatars();
|
|
|
|
expect(updateImage).toHaveBeenCalledTimes(2);
|
|
expect(updateProfile).toHaveBeenCalledTimes(2);
|
|
expect(cache.get('social.twitter.avatar')).toBe('halloween');
|
|
});
|
|
|
|
it('leaves Japanese profiles unchanged when Japanese credentials are configured', async () => {
|
|
vi.stubEnv('BLUESKY_JP_SERVICE', 'https://bsky.social');
|
|
vi.stubEnv('BLUESKY_JP_IDENTIFIER', 'jp.splatoon3.ink');
|
|
vi.stubEnv('BLUESKY_JP_PASSWORD', 'jp-credential');
|
|
|
|
const updateBlueskyProfile = vi.spyOn(BlueskyClient.prototype, 'updateProfile').mockResolvedValue();
|
|
|
|
await updateAvatars();
|
|
|
|
expect(updateProfile).toHaveBeenCalledExactlyOnceWith({ name: 'Splatoon3.eek 🎃' });
|
|
expect(updateBlueskyProfile).not.toHaveBeenCalled();
|
|
expect([...cache.keys()]).toEqual(['social.twitter.avatar']);
|
|
});
|
|
});
|