mirror of
https://github.com/misenhower/splatoon3.ink.git
synced 2026-11-18 16:58:18 -06:00
A small Vue + Tailwind app in workers/updater/admin, served by the updater Worker on its own hostname (admin.splatoon3.ink, admin.dev.splatoon3.ink) behind Cloudflare Access: - Timeline: both lanes, credential renewals, and social posts on one time axis, a filterable run list, run details with per-updater timings and the run's own log lines, lane controls (run now, pause, scheduling), credential renewal, and a combined text log. - Social: the post log with errors, per-account posting switches, when each status last went to each account, and a preview that can post one status by hand after a confirmation. - Data: freshness of the core data files, the published file list, a check that the site serves the new data, and a cache purge. Worker side: - src/admin: the Access JWT check (jose; fails closed and only answers on ADMIN_HOSTNAME) and the API. Actions must be same-origin JSON POSTs, and the app is served with a strict CSP from the ADMIN_ASSETS binding. - Runs capture their console output (AsyncLocalStorage), so log lines belong to the run that printed them. The Scheduler keeps a post log and serves run details and log lines; RegionAuth keeps its last 24 renewals. - A manual run from the panel only posts to social when asked to. Shared code: - StatusGeneratorManager records what was posted or failed, and skips accounts turned off in private settings/social.json (the Node runner honours this too). app/social/admin.mjs previews and posts one status. - Updater results include their duration. Also: npm run admin:dev runs the panel against a simulated API, deploy scripts build the panel first, the dev environment's assets move to dev-assets.splatoon3.ink, and the lockfile drops entries nothing depends on any more. Not yet run against a deployed Worker or a real Access application. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
217 lines
9.5 KiB
JavaScript
217 lines
9.5 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import ValueCache from '../common/ValueCache.mjs';
|
|
import ScreenshotHelper from '../screenshots/ScreenshotHelper.mjs';
|
|
import StatusGeneratorManager from './StatusGeneratorManager.mjs';
|
|
import { readSocialSettings, setClientEnabled } from './SocialSettings.mjs';
|
|
import { lastPosted, postStatus, previewStatus, setAccountEnabled, socialAccounts, socialGenerators } from './admin.mjs';
|
|
import BlueskyClient from './clients/BlueskyClient.mjs';
|
|
import MastodonClient from './clients/MastodonClient.mjs';
|
|
import SchedulesStatus from './generators/SchedulesStatus.mjs';
|
|
import StatusGenerator from './generators/StatusGenerator.mjs';
|
|
|
|
vi.mock('../common/prefixedConsole.mjs', () => ({
|
|
default: () => ({ log: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),
|
|
}));
|
|
vi.mock('@sentry/core', () => ({ captureException: vi.fn() }));
|
|
|
|
const image = new Uint8Array([1, 2, 3]);
|
|
|
|
function credentials() {
|
|
vi.stubEnv('MASTODON_URL', 'https://mastodon.test');
|
|
vi.stubEnv('MASTODON_ACCESS_TOKEN', 'token');
|
|
vi.stubEnv('BLUESKY_SERVICE', 'https://bsky.test');
|
|
vi.stubEnv('BLUESKY_IDENTIFIER', 'en.test');
|
|
vi.stubEnv('BLUESKY_PASSWORD', 'password');
|
|
vi.stubEnv('BLUESKY_JP_SERVICE', 'https://bsky.test');
|
|
vi.stubEnv('BLUESKY_JP_IDENTIFIER', 'jp.test');
|
|
vi.stubEnv('BLUESKY_JP_PASSWORD', 'password');
|
|
}
|
|
|
|
// A status that renders without published data or a browser.
|
|
function fakeStatus(text = 'Rotation') {
|
|
vi.spyOn(SchedulesStatus.prototype, 'getStatus').mockImplementation(async function () {
|
|
return { status: text, locale: this.locale, media: [{ type: 'image/png', altText: 'Stages', file: image }] };
|
|
});
|
|
vi.spyOn(StatusGenerator.prototype, 'getDataTime').mockResolvedValue(1000);
|
|
vi.spyOn(SchedulesStatus.prototype, 'getDataTime').mockResolvedValue(1000);
|
|
vi.spyOn(ScreenshotHelper.prototype, 'open').mockResolvedValue();
|
|
vi.spyOn(ScreenshotHelper.prototype, 'close').mockResolvedValue();
|
|
}
|
|
|
|
beforeEach(() => {
|
|
for (let name of ['BUFFER_API_KEY', 'THREADS_USER_ID', 'MASTODON_URL', 'BLUESKY_SERVICE', 'BLUESKY_JP_SERVICE']) {
|
|
vi.stubEnv(name, '');
|
|
}
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe('accounts', () => {
|
|
it('lists every account with its credentials and posting switch, and no file writers', async () => {
|
|
credentials();
|
|
await setClientEnabled('mastodon', false);
|
|
|
|
let accounts = await socialAccounts();
|
|
|
|
expect(accounts.map(account => account.key)).toEqual(['twitter', 'mastodon', 'bluesky', 'threads', 'twitter.jp', 'bluesky.jp']);
|
|
expect(accounts.find(account => account.key === 'mastodon')).toMatchObject({ configured: true, enabled: false, locale: 'en-US' });
|
|
expect(accounts.find(account => account.key === 'mastodon').disabledAt).toEqual(expect.any(Number));
|
|
expect(accounts.find(account => account.key === 'twitter')).toMatchObject({ configured: false, enabled: true });
|
|
expect(accounts.find(account => account.key === 'bluesky.jp')).toMatchObject({ configured: true, locale: 'ja-JP' });
|
|
});
|
|
|
|
it('turns an account off and on again, and refuses unknown accounts', async () => {
|
|
await setAccountEnabled('threads', false);
|
|
expect(Object.keys((await readSocialSettings()).disabledClients)).toEqual(['threads']);
|
|
|
|
await setAccountEnabled('threads', true);
|
|
expect((await readSocialSettings()).disabledClients).toEqual({});
|
|
|
|
await expect(setAccountEnabled('image', false)).rejects.toThrow('Unknown account: image');
|
|
});
|
|
|
|
it('gives a turned-off account no scheduled posts and leaves its delivery marker alone', async () => {
|
|
credentials();
|
|
fakeStatus();
|
|
await setClientEnabled('mastodon', false);
|
|
|
|
let mastodon = vi.spyOn(MastodonClient.prototype, 'send').mockResolvedValue();
|
|
let bluesky = vi.spyOn(BlueskyClient.prototype, 'send').mockResolvedValue();
|
|
let manager = new StatusGeneratorManager([new SchedulesStatus], [new MastodonClient, new BlueskyClient]);
|
|
|
|
let events = await manager.sendStatuses();
|
|
|
|
expect(mastodon).not.toHaveBeenCalled();
|
|
expect(bluesky).toHaveBeenCalledOnce();
|
|
expect(events.map(event => [event.type, event.generator.key, event.client.key])).toEqual([['posted', 'schedules', 'bluesky']]);
|
|
expect(await new ValueCache('social.mastodon.schedules').getData()).toBeNull();
|
|
expect(await new ValueCache('social.bluesky.schedules').getData()).toBe(1000);
|
|
});
|
|
});
|
|
|
|
describe('post log events', () => {
|
|
it('records a failed post with its error and keeps going', async () => {
|
|
credentials();
|
|
fakeStatus();
|
|
vi.spyOn(MastodonClient.prototype, 'send').mockRejectedValue(new Error('HTTP 502'));
|
|
vi.spyOn(BlueskyClient.prototype, 'send').mockResolvedValue();
|
|
|
|
let manager = new StatusGeneratorManager([new SchedulesStatus], [new MastodonClient, new BlueskyClient]);
|
|
let events = await manager.sendStatuses();
|
|
|
|
expect(events.map(event => [event.type, event.client.key, event.error])).toEqual(expect.arrayContaining([
|
|
['error', 'mastodon', 'HTTP 502'],
|
|
['posted', 'bluesky', undefined],
|
|
]));
|
|
expect(events).toHaveLength(2);
|
|
});
|
|
|
|
it('records a status that could not be generated', async () => {
|
|
credentials();
|
|
vi.spyOn(SchedulesStatus.prototype, 'shouldPost').mockResolvedValue(true);
|
|
vi.spyOn(SchedulesStatus.prototype, 'getStatus').mockRejectedValue(new Error('Screenshot timed out'));
|
|
vi.spyOn(ScreenshotHelper.prototype, 'open').mockResolvedValue();
|
|
vi.spyOn(ScreenshotHelper.prototype, 'close').mockResolvedValue();
|
|
|
|
let events = await new StatusGeneratorManager([new SchedulesStatus], [new BlueskyClient]).sendStatuses();
|
|
|
|
expect(events).toEqual([expect.objectContaining({ type: 'error', client: null, error: 'Screenshot timed out', generator: { key: 'schedules', name: 'Schedules' } })]);
|
|
});
|
|
});
|
|
|
|
describe('last posted', () => {
|
|
it('reads when each status last went to each account', async () => {
|
|
await new ValueCache('social.bluesky.schedules').setData(1000);
|
|
await new ValueCache('social.bluesky.jp.gear.dailydrop').setData(2000);
|
|
|
|
let posted = await lastPosted();
|
|
|
|
expect(Object.keys(posted).sort()).toEqual(['bluesky', 'bluesky.jp']);
|
|
expect(posted.bluesky.schedules.postedAt).toEqual(expect.any(Number));
|
|
expect(Object.keys(posted['bluesky.jp'])).toEqual(['gear.dailydrop']);
|
|
});
|
|
|
|
it('lists the statuses in posting order', () => {
|
|
expect(socialGenerators()[0]).toEqual({ key: 'schedules', name: 'Schedules' });
|
|
expect(socialGenerators().map(generator => generator.key)).toContain('gear.dailydrop');
|
|
});
|
|
});
|
|
|
|
describe('preview', () => {
|
|
it('renders the status without sending or marking anything', async () => {
|
|
credentials();
|
|
fakeStatus('ローテーション');
|
|
|
|
let send = vi.spyOn(BlueskyClient.prototype, 'send');
|
|
let preview = await previewStatus('schedules', 'ja-JP');
|
|
|
|
expect(preview).toEqual({ text: 'ローテーション', contentWrapper: null, media: [{ type: 'image/png', altText: 'Stages', file: image }] });
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(await lastPosted()).toEqual({});
|
|
});
|
|
|
|
it('refuses unknown statuses and statuses with nothing to say', async () => {
|
|
await expect(previewStatus('nope')).rejects.toThrow('Unknown status: nope');
|
|
|
|
fakeStatus(false);
|
|
await expect(previewStatus('schedules')).rejects.toThrow('nothing to post');
|
|
});
|
|
});
|
|
|
|
describe('posting by hand', () => {
|
|
it('sends one status to the chosen accounts in their own language, even when not due', async () => {
|
|
credentials();
|
|
fakeStatus();
|
|
await new ValueCache('social.bluesky.schedules').setData(1000); // already posted for this rotation
|
|
|
|
let sent = [];
|
|
vi.spyOn(BlueskyClient.prototype, 'send').mockImplementation(async function (status) {
|
|
sent.push([this.key, status.locale]);
|
|
});
|
|
let mastodon = vi.spyOn(MastodonClient.prototype, 'send').mockResolvedValue();
|
|
|
|
let events = await postStatus('schedules', ['bluesky', 'bluesky.jp']);
|
|
|
|
expect(sent.sort()).toEqual([['bluesky', 'en-US'], ['bluesky.jp', 'ja-JP']]);
|
|
expect(mastodon).not.toHaveBeenCalled();
|
|
expect(events.map(event => [event.type, event.client.key]).sort()).toEqual([['posted', 'bluesky'], ['posted', 'bluesky.jp']]);
|
|
});
|
|
|
|
it('refuses a turned-off account instead of posting to it', async () => {
|
|
credentials();
|
|
fakeStatus();
|
|
await setClientEnabled('bluesky', false);
|
|
|
|
let send = vi.spyOn(BlueskyClient.prototype, 'send').mockResolvedValue();
|
|
let events = await postStatus('schedules', ['bluesky']);
|
|
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(events).toEqual([expect.objectContaining({ type: 'error', client: { key: 'bluesky', name: 'Bluesky' }, error: 'Account is turned off or has no credentials' })]);
|
|
});
|
|
|
|
it('never posts a status that has no text', async () => {
|
|
credentials();
|
|
fakeStatus(false);
|
|
|
|
let send = vi.spyOn(BlueskyClient.prototype, 'send').mockResolvedValue();
|
|
let events = await postStatus('schedules', ['bluesky']);
|
|
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(events).toEqual([expect.objectContaining({ type: 'error', client: null, error: 'There is nothing to post for this status right now' })]);
|
|
});
|
|
|
|
it('refuses unknown accounts and file writers before doing anything', async () => {
|
|
credentials();
|
|
fakeStatus();
|
|
|
|
let send = vi.spyOn(BlueskyClient.prototype, 'send').mockResolvedValue();
|
|
|
|
await expect(postStatus('schedules', ['bluesky', 'image'])).rejects.toThrow('Unknown account: image');
|
|
await expect(postStatus('schedules', [])).rejects.toThrow('Choose at least one account');
|
|
expect(send).not.toHaveBeenCalled();
|
|
});
|
|
});
|