const { test, expect, describe } = require('bun:test'); const { basePathOf, underBasePath, requestUri } = require('./base-path'); describe('basePathOf — the path the backend is served under', () => { test('a backend owning its origin is served at the root, with or without a trailing slash', () => { expect(basePathOf('http://localhost:3003')).toBe('/'); expect(basePathOf('http://localhost:3003/')).toBe('/'); }); test('a backend sharing the front end origin is served under its path, always ending with a slash', () => { expect(basePathOf('https://festipod.dev.reconnexion.coop/api/')).toBe('/api/'); expect(basePathOf('https://festipod.dev.reconnexion.coop/api')).toBe('/api/'); }); test('an unset base URL reads as the root', () => { expect(basePathOf(undefined)).toBe('/'); expect(basePathOf('')).toBe('/'); }); }); describe('underBasePath — where a route is mounted', () => { test('at the root, a route keeps its path', () => { expect(underBasePath('/', '/catalog')).toBe('/catalog'); expect(underBasePath('/', '/as/actor/app')).toBe('/as/actor/app'); }); test('under a base path, a route is prefixed by it, without a doubled slash', () => { expect(underBasePath('/api/', '/catalog')).toBe('/api/catalog'); expect(underBasePath('/api/', 'as/actor/app')).toBe('/api/as/actor/app'); }); }); describe('requestUri — the URI a request was made to', () => { test('at the root, the request path is appended to the origin', () => { expect(requestUri('http://localhost:3003', '/as/actor/app')).toBe('http://localhost:3003/as/actor/app'); expect(requestUri('http://localhost:3003/', '/app')).toBe('http://localhost:3003/app'); }); test('under a base path, the raw request path already carries it and is not prefixed twice', () => { expect(requestUri('https://festipod.dev.reconnexion.coop/api/', '/api/as/actor/app')).toBe( 'https://festipod.dev.reconnexion.coop/api/as/actor/app' ); expect(requestUri('https://festipod.dev.reconnexion.coop/api', '/api/app')).toBe( 'https://festipod.dev.reconnexion.coop/api/app' ); }); test('the base URL is kept exactly as written — the same URI the backend mints its actor under', () => { const urlJoin = require('url-join'); for (const baseUrl of ['http://localhost:80', 'https://Festipod.Example/api/', 'https://host:443/api']) { const path = `${basePathOf(baseUrl)}as/actor/app`; expect(requestUri(baseUrl, path)).toBe(urlJoin(baseUrl, 'as/actor/app')); } }); });