api: /api/version + OpenAPI 3.x spec generated in code

lib/openapi.ts is the machine-readable spec (served at /api/openapi) per AGENTS.md;
info.version sourced from package.json. Unit tests pin the spec shape and version sync.
This commit is contained in:
2026-09-28 15:56:29 +00:00
parent e688e9a0f0
commit 323b00d692
4 changed files with 84 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
import pkg from '@/package.json';
import { describe, expect, it } from 'vitest';
import { openApiSpec } from './openapi';
describe('OpenAPI spec', () => {
it('documents exactly the one public endpoint (scaffold stage)', () => {
expect(Object.keys(openApiSpec.paths).sort()).toEqual(['/api/version']);
});
it('keeps info.version in sync with package.json', () => {
expect(openApiSpec.info.title).toBe('SpaceWanderer API');
expect(openApiSpec.info.version).toBe(pkg.version);
});
it('uses an empty server url so "try it" targets the current origin', () => {
expect(openApiSpec.servers).toEqual([{ url: '' }]);
});
it('describes /api/version as { version } with a string version', () => {
const schema = openApiSpec.paths['/api/version'].get.responses[200].content['application/json'].schema;
expect(schema.$ref).toBe('#/components/schemas/AppVersion');
const appVersion = openApiSpec.components.schemas.AppVersion.properties;
expect(Object.keys(appVersion)).toEqual(['version']);
expect(appVersion.version.type).toBe('string');
});
});