Files
scrabble-game/ui/src/lib/share.test.ts
T
developer d5fbaa3034
CI / changes (push) Successful in 1s
CI / unit (push) Successful in 9s
CI / integration (push) Successful in 15s
CI / ui (push) Successful in 1m2s
CI / conformance (push) Successful in 9s
CI / gate (push) Successful in 0s
CI / deploy (push) Successful in 1m42s
feat(export): server-rendered artifacts behind one signed download URL (#160)
The finished-game export (GCG + a new PNG of the final position) is one
signed, short-lived relative URL (game.export_url; HMAC-SHA256, 10-min
TTL, BACKEND_EXPORT_SIGN_KEY) resolved against the client's own origin
and delivered by the best affordance each platform has (five on-device
review rounds):

- TG Android/desktop: native showPopup chooser -> native downloadFile
  dialog (bridge-only chain, activation-safe).
- TG iOS: app-modal chooser -> OS share sheet with the fetched file
  (a popup callback cannot supply the activation the sheet needs).
- VK iOS: VKWebAppDownloadFile for both formats.
- VK Android: the PNG opens in VK's native image viewer, the GCG copies
  to the clipboard (the VK Android downloader hangs on any download,
  Content-Length/Range notwithstanding).
- VK desktop iframe / desktop browsers: plain anchor downloads.
- Mobile browsers: the OS share sheet (fetch-then-share).
- Legacy TG (< Bot API 8.0): app modal + GCG clipboard, no image option.

The PNG is rasterized on demand by the new internal `renderer` sidecar
(node:22-slim + skia-canvas + baked Liberation/Noto Color Emoji fonts)
executing the SAME ui/src/lib/gameimage.ts the ui project unit-tests;
the backend rebuilds the render payload from the journal +
engine.AlphabetTable, and the device date locale, IANA time zone and
localized non-play labels ride the signed URL. Nothing is stored — the
artifact re-derives from the immutable journal on each GET. The gateway
forwards /dl/* (caddy @gateway matcher extended) behind the per-IP
public rate limiter and serves bytes via http.ServeContent.

Deploy: renderer service in compose + prod overlay + rolling order +
prod push list; TEST_/PROD_EXPORT_SIGN_KEY secrets; the sidecar smoke
runs in the ui CI job. Docs: ARCHITECTURE, FUNCTIONAL(+_ru), UI_DESIGN,
TESTING, deploy/README, renderer/README.
2026-07-02 21:58:07 +00:00

142 lines
6.0 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { downloadUrl, pickGcgDelivery, pickTextShare, shareOrDownloadGcg, shareText } from './share';
import type { GcgExport } from './model';
const file = {} as File;
const gcg: GcgExport = { gameId: 'g1', filename: 'game.gcg', content: '#title game' };
describe('pickGcgDelivery', () => {
const canShareNav = { canShare: () => true, share: async () => {} };
const noShareNav = { canShare: () => false, share: async () => {} };
it('shares when the platform can share files, even in an in-app webview (iOS)', () => {
expect(pickGcgDelivery(canShareNav, file, false)).toBe('share');
expect(pickGcgDelivery(canShareNav, file, true)).toBe('share');
});
it('copies in an in-app webview that cannot share files (Android Telegram/VK: no share, dead download)', () => {
expect(pickGcgDelivery(noShareNav, file, true)).toBe('copy');
expect(pickGcgDelivery(undefined, file, true)).toBe('copy');
expect(pickGcgDelivery({ canShare: () => true } as never, file, true)).toBe('copy');
});
it('downloads on a plain browser without Web Share (desktop)', () => {
expect(pickGcgDelivery(noShareNav, file, false)).toBe('download');
expect(pickGcgDelivery(undefined, file, false)).toBe('download');
});
});
describe('shareOrDownloadGcg', () => {
afterEach(() => vi.unstubAllGlobals());
const noCopy = async () => false;
function stubDownloadEnv(canShare: boolean, share: () => Promise<void>) {
const anchor = { href: '', download: '', click: vi.fn(), remove: vi.fn() };
const createElement = vi.fn(() => anchor);
vi.stubGlobal('File', class {});
vi.stubGlobal('Blob', class {});
vi.stubGlobal('navigator', { canShare: () => canShare, share });
vi.stubGlobal('document', { createElement, body: { appendChild: vi.fn() } });
vi.stubGlobal('URL', { createObjectURL: vi.fn(() => 'blob:x'), revokeObjectURL: vi.fn() });
return { anchor, createElement };
}
it('never falls back to the navigating Blob download when a share is cancelled', async () => {
// Reproduces the iOS Telegram Mini App break: Web Share is available and the user cancels it
// (AbortError). The <a download> fallback navigates the WKWebView to the blob: URL and strands
// the app, so a cancelled (or failed) share must do nothing here.
const share = vi.fn().mockRejectedValue(new DOMException('cancelled', 'AbortError'));
const { createElement } = stubDownloadEnv(true, share);
expect(await shareOrDownloadGcg(gcg, false, noCopy)).toBe('shared');
expect(share).toHaveBeenCalledOnce();
expect(createElement).not.toHaveBeenCalled(); // no download anchor → no webview navigation
});
it('downloads via an anchor on a desktop browser that cannot share files', async () => {
const { anchor, createElement } = stubDownloadEnv(false, vi.fn());
expect(await shareOrDownloadGcg(gcg, false, noCopy)).toBe('downloaded');
expect(createElement).toHaveBeenCalledWith('a');
expect(anchor.click).toHaveBeenCalledOnce();
});
it('copies to the clipboard in an in-app webview that cannot share files (no dead Blob download)', async () => {
// Android Telegram/VK expose no Web Share AND ignore <a download>, so the export must copy the
// GCG text instead of silently issuing an anchor click that does nothing.
const copy = vi.fn().mockResolvedValue(true);
const { createElement } = stubDownloadEnv(false, vi.fn());
expect(await shareOrDownloadGcg(gcg, true, copy)).toBe('copied');
expect(copy).toHaveBeenCalledWith(gcg.content);
expect(createElement).not.toHaveBeenCalled();
});
it('reports failure when the in-app clipboard copy fails', async () => {
stubDownloadEnv(false, vi.fn());
expect(await shareOrDownloadGcg(gcg, true, async () => false)).toBe('failed');
});
});
describe('downloadUrl', () => {
afterEach(() => vi.unstubAllGlobals());
it('clicks a temporary anchor carrying the URL and the filename', () => {
const anchor = { href: '', download: '', click: vi.fn(), remove: vi.fn() };
const createElement = vi.fn(() => anchor);
vi.stubGlobal('document', { createElement, body: { appendChild: vi.fn() } });
downloadUrl('https://example.test/dl/g-1/png?e=1&s=x', 'game-1.png');
expect(createElement).toHaveBeenCalledWith('a');
expect(anchor.href).toBe('https://example.test/dl/g-1/png?e=1&s=x');
expect(anchor.download).toBe('game-1.png');
expect(anchor.click).toHaveBeenCalledOnce();
expect(anchor.remove).toHaveBeenCalledOnce();
});
});
describe('pickTextShare', () => {
it('shares when Web Share is available', () => {
expect(pickTextShare({ share: async () => {}, canShare: () => true })).toBe('share');
});
it('shares when share exists without canShare (text needs no file capability check)', () => {
expect(pickTextShare({ share: async () => {} })).toBe('share');
});
it('copies when there is no Web Share (desktop)', () => {
expect(pickTextShare(undefined)).toBe('copy');
expect(pickTextShare({} as never)).toBe('copy');
});
});
describe('shareText', () => {
afterEach(() => vi.unstubAllGlobals());
it('uses the OS share sheet when available', async () => {
const share = vi.fn().mockResolvedValue(undefined);
vi.stubGlobal('navigator', { share, canShare: () => true });
expect(await shareText('diag', 'title')).toBe('shared');
expect(share).toHaveBeenCalledWith({ title: 'title', text: 'diag' });
});
it('copies to the clipboard when Web Share is absent (desktop)', async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
vi.stubGlobal('navigator', { clipboard: { writeText } });
expect(await shareText('diag', 'title')).toBe('copied');
expect(writeText).toHaveBeenCalledWith('diag');
});
it('reports failure without a fallback when the share is cancelled', async () => {
const share = vi.fn().mockRejectedValue(new DOMException('cancelled', 'AbortError'));
vi.stubGlobal('navigator', { share, canShare: () => true });
expect(await shareText('diag', 'title')).toBe('failed');
});
});