import { afterAll, beforeAll, describe, expect, test } from 'bun:test'; import { readFileSync } from 'node:fs'; import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { SITE_LANGUAGES, createApp } from './app'; let dir: string; const HOTFIX_BODY = '{"version":3,"expires":"2099-01-01T00:00:00Z","minNative":"0.2.0","selectors":{"composer":"div"},"flags":{}}'; beforeAll(async () => { dir = await mkdtemp(join(tmpdir(), 'bt-api-')); await mkdir(join(dir, 'native'), { recursive: true }); await writeFile(join(dir, 'native', 'latest.json'), JSON.stringify({ version: '0.3.0', notes: 'Tabs and splits', pubDate: '2026-09-27T12:00:00Z', platforms: { 'windows-x86_64': { url: 'https://example.invalid/Better-Twttr_0.3.0_x64.zip', signature: 'c2ln' } }, })); await mkdir(join(dir, 'hotfix'), { recursive: true }); await writeFile(join(dir, 'hotfix', 'stable.json'), HOTFIX_BODY); await writeFile(join(dir, 'hotfix', 'stable.json.sig'), 'c2lnbmF0dXJl'); await writeFile(join(dir, 'hotfix', 'beta.json'), '{"version":1,"expires":"2099-01-01T00:00:00Z","minNative":"0.1.0","script":"alert(1)"}'); await writeFile(join(dir, 'hotfix', 'beta.json.sig'), 'c2ln'); }); afterAll(() => rm(dir, { recursive: true, force: true })); describe('update endpoint', () => { test('returns the Tauri update payload for an older client', async () => { const res = await createApp({ releasesDir: dir }).request('/v1/updates/windows/x86_64/0.2.0'); expect(res.status).toBe(200); expect(await res.json()).toEqual({ version: '0.3.0', notes: 'Tabs and splits', pub_date: '2026-09-27T12:00:00Z', url: 'https://example.invalid/Better-Twttr_0.3.0_x64.zip', signature: 'c2ln' }); }); test('returns 204 when up to date or the platform is unpublished', async () => { const app = createApp({ releasesDir: dir }); expect((await app.request('/v1/updates/windows/x86_64/0.3.0')).status).toBe(204); expect((await app.request('/v1/updates/darwin/aarch64/0.2.0')).status).toBe(204); }); test('rejects malformed parameters', async () => { expect((await createApp({ releasesDir: dir }).request('/v1/updates/windows/x86_64/latest')).status).toBe(400); }); test('returns 204 when nothing has been published yet', async () => { expect((await createApp({ releasesDir: join(dir, 'missing') }).request('/v1/updates/windows/x86_64/0.1.0')).status).toBe(204); }); test('works under a base path', async () => { const app = createApp({ releasesDir: dir, basePath: '/x/api' }); expect((await app.request('/x/api/health')).status).toBe(200); }); }); describe('download redirect', () => { test('302s to the current Windows installer', async () => { const res = await createApp({ releasesDir: dir }).request('/v1/download/windows/latest'); expect(res.status).toBe(302); expect(res.headers.get('location')).toBe('https://example.invalid/Better-Twttr_0.3.0_x64.zip'); }); test('404s when nothing is published for the target', async () => { expect((await createApp({ releasesDir: dir }).request('/v1/download/darwin/latest')).status).toBe(404); }); }); describe('hotfix channel', () => { test('serves the published body byte-for-byte', async () => { const res = await createApp({ releasesDir: dir }).request('/v1/hotfix/stable'); expect(res.status).toBe(200); expect(res.headers.get('content-type')).toContain('application/json'); expect(res.headers.get('cache-control')).toBe('public, max-age=60'); expect(await res.text()).toBe(HOTFIX_BODY); }); test('serves the detached signature', async () => { const res = await createApp({ releasesDir: dir }).request('/v1/hotfix/stable.sig'); expect(res.status).toBe(200); expect(await res.text()).toBe('c2lnbmF0dXJl'); }); test('rejects unknown channels and path tricks', async () => { const app = createApp({ releasesDir: dir }); expect((await app.request('/v1/hotfix/nightly')).status).toBe(400); expect((await app.request('/v1/hotfix/..%2Fnative%2Flatest')).status).toBe(400); }); test('never serves a body that fails the schema', async () => { const app = createApp({ releasesDir: dir }); expect((await app.request('/v1/hotfix/beta')).status).toBe(404); expect((await app.request('/v1/hotfix/beta.sig')).status).toBe(404); }); test('404s when nothing is published', async () => { expect((await createApp({ releasesDir: join(dir, 'missing') }).request('/v1/hotfix/stable')).status).toBe(404); }); test('works under a base path', async () => { const res = await createApp({ releasesDir: dir, basePath: '/x/api' }).request('/x/api/v1/hotfix/stable.sig'); expect(res.status).toBe(200); }); }); describe('site pages', () => { test('/x gets its trailing slash (relative Location) and the API still wins over the site', async () => { const app = createApp({ releasesDir: dir, web: async c => c.text('site') }); const res = await app.request('/x'); expect(res.status).toBe(301); expect(res.headers.get('location')).toBe('x/'); expect(await (await app.request('/x/')).text()).toBe('site'); expect((await app.request('/v1/updates/windows/x86_64/0.2.0')).status).toBe(200); }); test('the old /twttr address moves to /x for good: the page, deep paths and queries', async () => { const at = (base: string, path: string) => new URL(path, `https://waybetter.fyi${base}`).href.slice('https://waybetter.fyi'.length); for (const basePath of ['', '/mounted']) { const app = createApp({ releasesDir: dir, basePath, web: async c => c.text('site') }); for (const [from, to] of [ ['/twttr', '/x/'], ['/twttr/', '/x/'], ['/twttr?ref=old', '/x/?ref=old'], ['/twttr/index.html', '/x/index.html'], ['/twttr/a/b/c.png?v=3', '/x/a/b/c.png?v=3'], ] as const) { const res = await app.request(`${basePath}${from}`); expect(res.status).toBe(301); expect(at(`${basePath}${from}`, res.headers.get('location')!)).toBe(`${basePath}${to}`); } } }); test('the store galleries get their trailing slash too', async () => { const app = createApp({ releasesDir: dir, web: async c => c.text('site') }); for (const page of ['themes', 'plugins']) { const res = await app.request(`/${page}`); expect(res.status).toBe(301); expect(res.headers.get('location')).toBe(`${page}/`); expect(await (await app.request(`/${page}/`)).text()).toBe('site'); } }); test('the Store: /store and its pages get their trailing slash, files are left to the site', async () => { const at = (base: string, path: string) => new URL(path, `https://waybetter.fyi${base}`).href.slice('https://waybetter.fyi'.length); for (const basePath of ['', '/mounted']) { const app = createApp({ releasesDir: dir, basePath, web: async c => c.text(`site ${c.req.path}`) }); for (const [from, to, status] of [ ['/store', '/store/', 301], ['/store/submit', '/store/submit/', 301], ['/store/themes/nord', '/store/themes/nord/', 301], ['/store/plugins/thread-reader', '/store/plugins/thread-reader/', 301], // One kind's listing is the storefront with that filter. ['/store/themes', '/store/?kind=theme', 302], ['/store/themes/', '/store/?kind=theme', 302], ['/store/plugins', '/store/?kind=plugin', 302], ['/store/plugins/', '/store/?kind=plugin', 302], ] as const) { const res = await app.request(`${basePath}${from}`); expect(res.status).toBe(status); expect(at(`${basePath}${from}`, res.headers.get('location')!)).toBe(`${basePath}${to}`); } // Pages, and the files the apps download: untouched, straight to the static handler. for (const path of [ '/store/', '/store/submit/', '/store/themes/nord/', '/store/plugins/thread-reader/', '/store/catalog.json', '/store/catalog.json.sig', '/store/themes/nord.json', '/store/plugins/bt-thread-reader.plugin.js', '/store/previews/theme-nord.svg', ]) { const res = await app.request(`${basePath}${path}`); expect(res.status).toBe(200); expect(await res.text()).toBe(`site ${basePath}${path}`); } } }); test('a missing page is the 404 page for browsers, and JSON for programs and the API', async () => { const page = join(dir, '404.html'); await writeFile(page, 'Page not found'); const html = { headers: { accept: 'text/html,application/xhtml+xml' } }; const app = createApp({ releasesDir: dir, notFoundPage: page, web: async (_c, next) => next() }); const res = await app.request('/store/themes/nope/', html); expect(res.status).toBe(404); expect(res.headers.get('content-type')).toContain('text/html'); expect(await res.text()).toContain('Page not found'); // The apps and the updater never ask for HTML: their answer stays JSON. const json = await app.request('/store/themes/nope.json'); expect(json.status).toBe(404); expect(await json.json()).toEqual({ error: 'not found' }); // API paths answer JSON even to a browser. const api = await app.request('/v1/nope', html); expect(api.status).toBe(404); expect(api.headers.get('content-type')).toContain('application/json'); // Under a path prefix the page's links still start at the site's root. const mounted = createApp({ releasesDir: dir, basePath: '/mounted', notFoundPage: page, web: async (_c, next) => next() }); expect(await (await mounted.request('/mounted/nope', html)).text()).toContain(' next() }).request('/nope', html); expect(bare.status).toBe(404); expect(await bare.json()).toEqual({ error: 'not found' }); }); }); describe('site languages', () => { const at = (base: string, path: string) => new URL(path, `https://waybetter.fyi${base}`).href.slice('https://waybetter.fyi'.length); const html = { headers: { accept: 'text/html,application/xhtml+xml' } }; test('the API knows exactly the languages the site is published in', () => { const web = join(import.meta.dir, '../../web'); const listed = /export const LANGUAGES = \[([^\]]+)\]/.exec(readFileSync(join(web, 'site.config.ts'), 'utf8'))![1]!.split(',').map(code => code.trim().replace(/'/g, '')); expect(listed[0]).toBe('en'); expect([...SITE_LANGUAGES]).toEqual(listed.slice(1)); }); test('/xx gets its trailing slash, and every page redirect exists under a language', async () => { for (const basePath of ['', '/mounted']) { const app = createApp({ releasesDir: dir, basePath, web: async c => c.text(`site ${c.req.path}`) }); for (const [from, to, status] of [ ['/tr', '/tr/', 301], ['/ar', '/ar/', 301], ['/zh-Hant', '/zh-Hant/', 301], ['/tr?ref=x', '/tr/', 301], ['/tr/x', '/tr/x/', 301], ['/ja/themes', '/ja/themes/', 301], ['/de/plugins', '/de/plugins/', 301], ['/tr/store', '/tr/store/', 301], ['/tr/store/submit', '/tr/store/submit/', 301], ['/hi/store/plugins/thread-reader', '/hi/store/plugins/thread-reader/', 301], ['/tr/store/themes', '/tr/store/?kind=theme', 302], ['/tr/store/plugins/', '/tr/store/?kind=plugin', 302], // Another spelling of a language's code moves to the folder's own, path and query kept. ['/TR', '/tr/', 301], ['/zh-hant/', '/zh-Hant/', 301], ['/zh-hant/store/themes/nord/?q=1', '/zh-Hant/store/themes/nord/?q=1', 301], ['/ZH-HANT/x/index.html', '/zh-Hant/x/index.html', 301], ] as const) { const res = await app.request(`${basePath}${from}`); expect([from, res.status]).toEqual([from, status]); expect([from, at(`${basePath}${from}`, res.headers.get('location')!)]).toEqual([from, `${basePath}${to}`]); } // The pages themselves, and folders that only look like a language, go straight to the site. for (const path of ['/tr/', '/tr/x/', '/zh-Hant/store/', '/ar/store/plugins/thread-reader/', '/og/home.png', '/x/']) { const res = await app.request(`${basePath}${path}`); expect([path, res.status, await res.text()]).toEqual([path, 200, `site ${basePath}${path}`]); } } }); test('an unknown language prefix is a 404, with or without the slash', async () => { const page = join(dir, '404.html'); await writeFile(page, 'Page not found'); const app = createApp({ releasesDir: dir, notFoundPage: page, web: async (_c, next) => next() }); for (const path of ['/xx', '/xx/', '/xx/store/', '/en', '/en/', '/en-GB/', '/zh-Hans/', '/pt-BR']) { const res = await app.request(path, html); expect([path, res.status]).toEqual([path, 404]); expect([path, res.headers.get('location')]).toEqual([path, null]); expect(await res.text()).toContain('Page not found'); } }); test('a missing page under a language is that language\'s 404 page', async () => { const page = join(dir, '404.html'); await writeFile(page, 'Page not found'); await mkdir(join(dir, 'tr'), { recursive: true }); await writeFile(join(dir, 'tr', '404.html'), 'Sayfa bulunamadı'); const app = createApp({ releasesDir: dir, notFoundPage: page, web: async (_c, next) => next() }); const res = await app.request('/tr/store/themes/nope/', html); expect(res.status).toBe(404); expect(await res.text()).toContain('Sayfa bulunamadı'); // A language whose page is not built yet, and English, answer with the English page. expect(await (await app.request('/ja/nope', html)).text()).toContain('Page not found'); expect(await (await app.request('/nope', html)).text()).toContain('Page not found'); // Programs keep the JSON answer. expect(await (await app.request('/tr/store/catalog.json')).json()).toEqual({ error: 'not found' }); // Under a path prefix the links still start at the language's home. const mounted = createApp({ releasesDir: dir, basePath: '/mounted', notFoundPage: page, web: async (_c, next) => next() }); expect(await (await mounted.request('/mounted/tr/nope', html)).text()).toContain('