Files
docs/packages/openapi/test/code-sample.test.ts
T
Fuma NamaandGitHub c88590b935 Openapi 3.1 (#1122)
* OpenAPI: update types

* improve OpenAPI 3.0 to 3.1 converter

* prepare for Scalar parser for 3.0 to 3.1 converter

* improve schema type utility

* Fix cyclic references without custom id/title

* Add scopes to auth schemes

* Support Webhooks

* Support generating webhooks docs

* fix tests

* Support operation callbacks

* prepare for merge

* fix conflicts

* make compatible with older OpenAPI versions

* fix issues
2024-11-30 17:00:24 +08:00

68 lines
2.0 KiB
TypeScript

import { fileURLToPath } from 'node:url';
import { describe, expect, test } from 'vitest';
import * as Python from '@/requests/python';
import * as Go from '@/requests/go';
import * as Curl from '@/requests/curl';
import * as JS from '@/requests/javascript';
import { generateSample } from '@/utils/generate-sample';
import { createMethod } from '@/server/create-method';
import { methodKeys } from '@/build-routes';
import { getContext } from '@/server/api-page';
import { processDocument } from '@/utils/process-document';
const example = fileURLToPath(
new URL('./fixtures/petstore.yaml', import.meta.url),
);
const processed = await processDocument(example, true);
processed.document.servers = [
{
url: 'http://localhost:8080',
},
];
const ctx = await getContext(processed, {
shikiOptions: {
theme: 'vitesse-dark',
},
});
const { document } = processed;
describe('Code Sample Generators', () => {
for (const [path, item] of Object.entries(document.paths ?? {})) {
if (!item) continue;
for (const method of methodKeys) {
const operation = item[method];
if (!operation) continue;
const info = createMethod(method, item, operation);
const endpoint = generateSample(path, info, ctx);
test(`Go: ${method} ${path}`, async () => {
await expect(Go.getSampleRequest(endpoint)).toMatchFileSnapshot(
`./out/samples/${path}/${method}.go`,
);
});
test(`Curl: ${method} ${path}`, async () => {
await expect(Curl.getSampleRequest(endpoint)).toMatchFileSnapshot(
`./out/samples/${path}/${method}.bash`,
);
});
test(`Python: ${method} ${path}`, async () => {
await expect(Python.getSampleRequest(endpoint)).toMatchFileSnapshot(
`./out/samples/${path}/${method}.py`,
);
});
test(`JavaScript: ${method} ${path}`, async () => {
await expect(JS.getSampleRequest(endpoint)).toMatchFileSnapshot(
`./out/samples/${path}/${method}.js`,
);
});
}
}
});