Release API Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
const ORIGIN = "http://127.0.0.1:4196";
|
||||
async function watchLocalOnly(page: Page) {
|
||||
const external: string[] = [];
|
||||
await page.route("**/*", async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
if (url.origin !== ORIGIN) {
|
||||
external.push(url.href);
|
||||
await route.abort();
|
||||
} else await route.continue();
|
||||
});
|
||||
return external;
|
||||
}
|
||||
|
||||
test("navigates and generates inert examples at a nested path", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.setViewportSize({ width: 1800, height: 1000 });
|
||||
const external = await watchLocalOnly(page);
|
||||
await page.goto("/deep/nested/api/");
|
||||
await expect(page.getByRole("heading", { name: "API Tools" })).toBeVisible();
|
||||
await expect(page.getByText(/2 operations/iu)).toBeVisible();
|
||||
await page.getByRole("button", { name: /PATCH.*\/books/iu }).click();
|
||||
await expect(page.getByText("Generated request body")).toBeVisible();
|
||||
await expect(page.getByLabel("curl")).toHaveValue(/curl --request PATCH/iu);
|
||||
await page.getByLabel("Search operations").fill("getBook");
|
||||
await expect(page.locator(".operation-list li")).toHaveCount(1);
|
||||
expect(external).toEqual([]);
|
||||
expect(
|
||||
await page
|
||||
.locator(".toolbox-shell__main")
|
||||
.evaluate((node) => getComputedStyle(node).width),
|
||||
).toBe("1440px");
|
||||
});
|
||||
|
||||
test("resolves only explicitly supplied local multi-file references", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/api/");
|
||||
const main = `openapi: 3.1.0\ninfo: {title: Multi, version: '1'}\npaths:\n /book:\n get:\n responses:\n '200':\n description: ok\n content:\n application/json:\n schema: {$ref: 'schemas.yaml#/Book'}\n`;
|
||||
const schemas = `Book: {type: object, properties: {title: {type: string, example: Remote Book}}}`;
|
||||
await page.getByTestId("api-file-input").setInputFiles([
|
||||
{
|
||||
name: "openapi.yaml",
|
||||
mimeType: "application/yaml",
|
||||
buffer: Buffer.from(main),
|
||||
},
|
||||
{
|
||||
name: "schemas.yaml",
|
||||
mimeType: "application/yaml",
|
||||
buffer: Buffer.from(schemas),
|
||||
},
|
||||
]);
|
||||
await expect(page.getByText(/1 supporting file/iu)).toBeVisible();
|
||||
await expect(page.getByText(/Remote Book/iu)).toBeVisible();
|
||||
await page.getByLabel("Entry description").selectOption("schemas.yaml");
|
||||
await expect(page.getByText(/3 errors/iu)).toBeVisible();
|
||||
await page.getByLabel("Entry description").selectOption("openapi.yaml");
|
||||
await expect(page.getByText(/Remote Book/iu)).toBeVisible();
|
||||
await page.getByRole("button", { name: "Source" }).click();
|
||||
await expect(
|
||||
page.getByRole("list").getByText("schemas.yaml", { exact: true }),
|
||||
).toBeVisible();
|
||||
});
|
||||
|
||||
test("retains a valid model, compares revisions and inspects HAR", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/api/");
|
||||
await page.getByLabel("OpenAPI source").fill('not: "unterminated');
|
||||
await page.getByRole("button", { name: "Analyse description" }).click();
|
||||
await expect(page.getByRole("alert")).toContainText("last successful");
|
||||
await expect(
|
||||
page.getByText("/books/{bookId}", { exact: true }).first(),
|
||||
).toBeVisible();
|
||||
await page.getByRole("button", { name: "Compare" }).click();
|
||||
await expect(page.getByText("Operation was added.")).toBeVisible();
|
||||
const pending = page.waitForEvent("download");
|
||||
await page.getByRole("button", { name: "Download JSON" }).click();
|
||||
expect((await pending).suggestedFilename()).toBe(
|
||||
"openapi-change-report.json",
|
||||
);
|
||||
await page.getByRole("button", { name: "Exchanges" }).click();
|
||||
await expect(
|
||||
page.getByRole("cell", { name: "https://api.example.test/books/book-42" }),
|
||||
).toBeVisible();
|
||||
await page
|
||||
.getByLabel("Saved HTTP exchange")
|
||||
.fill(
|
||||
"GET /health HTTP/1.1\nHost: example.test\n\n--- response ---\nHTTP/1.1 204 No Content\n",
|
||||
);
|
||||
await page.getByRole("button", { name: "Inspect saved exchange" }).click();
|
||||
await expect(page.getByRole("cell", { name: "/health" })).toBeVisible();
|
||||
});
|
||||
|
||||
test("integrates help, dark theme, PWA identity and hardened headers", async ({
|
||||
page,
|
||||
request,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/api/");
|
||||
await page.getByRole("button", { name: "Help" }).click();
|
||||
await expect(
|
||||
page.getByRole("dialog", { name: "About API Tools" }),
|
||||
).toContainText("never executes HTTP");
|
||||
await page.keyboard.press("Escape");
|
||||
await page.getByRole("button", { name: "Personalize" }).click();
|
||||
await page.getByRole("button", { name: "Dark" }).click();
|
||||
await expect(page.locator(".toolbox-shell").first()).toHaveAttribute(
|
||||
"data-toolbox-theme",
|
||||
"dark",
|
||||
);
|
||||
expect(
|
||||
await page.evaluate(async () =>
|
||||
Boolean(await navigator.serviceWorker.ready),
|
||||
),
|
||||
).toBe(true);
|
||||
const index = await request.get("/deep/nested/api/");
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"connect-src 'self'",
|
||||
);
|
||||
expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u);
|
||||
const manifest = await request.get("/deep/nested/api/toolbox-app.json");
|
||||
await expect(manifest.json()).resolves.toMatchObject({
|
||||
id: "de.add-ideas.api-tools",
|
||||
version: "0.1.0",
|
||||
privacy: { processing: "local", telemetry: false },
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Workbench } from "../../src/components/Workbench";
|
||||
|
||||
describe("API workbench", () => {
|
||||
it("navigates operations and retains a model after invalid source", async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Workbench />);
|
||||
expect(
|
||||
screen.getByRole("heading", { name: "API Tools" }),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getAllByText("GET", { exact: true })).toHaveLength(2);
|
||||
expect(screen.getByText(/curl --request GET/iu)).toBeInTheDocument();
|
||||
await user.clear(screen.getByLabelText("OpenAPI source"));
|
||||
await user.type(
|
||||
screen.getByLabelText("OpenAPI source"),
|
||||
'not: "unterminated',
|
||||
);
|
||||
await user.click(
|
||||
screen.getByRole("button", { name: "Analyse description" }),
|
||||
);
|
||||
expect(screen.getByRole("alert")).toHaveTextContent(/last successful/iu);
|
||||
expect(
|
||||
screen.getAllByText("/books/{bookId}", { exact: true }),
|
||||
).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("shows security, comparison and exchange workspaces", async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Workbench />);
|
||||
await user.click(screen.getByRole("button", { name: "Security" }));
|
||||
expect(screen.getByText("bearerAuth", { exact: true })).toBeInTheDocument();
|
||||
await user.click(screen.getByRole("button", { name: "Compare" }));
|
||||
expect(screen.getByText(/Operation was added/iu)).toBeInTheDocument();
|
||||
await user.click(screen.getByRole("button", { name: "Exchanges" }));
|
||||
expect(
|
||||
screen.getByRole("cell", {
|
||||
name: "https://api.example.test/books/book-42",
|
||||
}),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { compareApis } from "../../src/core/compare";
|
||||
import { inspectHar, inspectRawExchange } from "../../src/core/har";
|
||||
import {
|
||||
collectOperations,
|
||||
operationExample,
|
||||
securityInventory,
|
||||
} from "../../src/core/operations";
|
||||
import { parseApiDocument } from "../../src/core/parse";
|
||||
import { createWorkspace } from "../../src/core/refs";
|
||||
|
||||
const SPEC = `openapi: 3.1.0
|
||||
info: { title: T, version: '1' }
|
||||
servers: [{ url: "https://api.example.test" }]
|
||||
paths:
|
||||
/items/{id}:
|
||||
get:
|
||||
operationId: readItem
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string, example: "a'b" } }
|
||||
responses:
|
||||
"200": { description: ok, content: { application/json: { schema: { type: object, properties: { id: {type: string} } } } } }
|
||||
components:
|
||||
securitySchemes: { key: { type: apiKey, in: header, name: X-Key } }
|
||||
`;
|
||||
|
||||
describe("operation derivation, comparisons and saved exchanges", () => {
|
||||
it("collects operations, schemes and safely quoted inert commands", () => {
|
||||
const document = parseApiDocument(SPEC, "openapi.yaml");
|
||||
const operations = collectOperations(document.value);
|
||||
expect(operations.map((item) => item.key)).toEqual(["GET /items/{id}"]);
|
||||
const example = operationExample(createWorkspace(document), operations[0]!);
|
||||
expect(example.url).toContain("a'b");
|
||||
expect(example.commands.curl).toContain("curl --request GET");
|
||||
expect(example.commands.fetch).toContain("fetch(");
|
||||
expect(securityInventory(document.value)).toEqual([
|
||||
{ name: "key", type: "apiKey", detail: "header · X-Key" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("reports a missing local reference without aborting generation", () => {
|
||||
const document = parseApiDocument(
|
||||
"openapi: 3.1.0\ninfo: {title: Missing, version: '1'}\npaths: {/x: {get: {responses: {'200': {description: ok, content: {application/json: {schema: {$ref: 'missing.yaml#/Result'}}}}}}}}",
|
||||
"openapi.yaml",
|
||||
);
|
||||
const operation = collectOperations(document.value)[0]!;
|
||||
const example = operationExample(createWorkspace(document), operation);
|
||||
expect(example.response).toBeUndefined();
|
||||
expect(example.notices.join(" ")).toMatch(/missing\.yaml/iu);
|
||||
});
|
||||
|
||||
it("classifies removed operations and additions", () => {
|
||||
const before = parseApiDocument(SPEC, "before.yaml");
|
||||
const after = parseApiDocument(
|
||||
SPEC.replace(" get:", " post:"),
|
||||
"after.yaml",
|
||||
);
|
||||
expect(compareApis(before, after)).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ level: "breaking", path: "GET /items/{id}" }),
|
||||
expect.objectContaining({
|
||||
level: "non-breaking",
|
||||
path: "POST /items/{id}",
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("summarizes HAR and raw exchanges without exposing headers", () => {
|
||||
const har = inspectHar(
|
||||
'{"log":{"entries":[{"time":7,"request":{"method":"POST","url":"https://example.test/x","headers":[{"name":"Authorization","value":"secret"}],"postData":{"text":"abc"}},"response":{"status":201,"headers":[],"content":{"size":9,"mimeType":"application/json"}}}]}}',
|
||||
);
|
||||
expect(har[0]).toMatchObject({
|
||||
method: "POST",
|
||||
status: 201,
|
||||
requestHeaders: 1,
|
||||
requestBytes: 3,
|
||||
responseBytes: 9,
|
||||
});
|
||||
expect(JSON.stringify(har)).not.toContain("secret");
|
||||
expect(
|
||||
inspectRawExchange(
|
||||
"GET /x HTTP/1.1\r\nHost: example.test\r\n\r\n--- response ---\r\nHTTP/1.1 204 No Content\r\nX-Test: yes\r\n",
|
||||
)[0],
|
||||
).toMatchObject({ method: "GET", url: "/x", status: 204 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseApiDocument, validateOpenApi } from "../../src/core/parse";
|
||||
import {
|
||||
createWorkspace,
|
||||
generateSchemaSample,
|
||||
resolveLocalReference,
|
||||
} from "../../src/core/refs";
|
||||
|
||||
describe("OpenAPI parsing and local references", () => {
|
||||
it("parses JSON and YAML and performs focused validation", () => {
|
||||
const json = parseApiDocument(
|
||||
'{"openapi":"3.0.3","info":{"title":"A","version":"1"},"paths":{}}',
|
||||
"api.json",
|
||||
);
|
||||
const yaml = parseApiDocument(
|
||||
"openapi: 3.1.0\ninfo: { title: A, version: '1' }\npaths: {}\n",
|
||||
"api.yaml",
|
||||
);
|
||||
expect(json.format).toBe("json");
|
||||
expect(yaml.format).toBe("yaml");
|
||||
expect(validateOpenApi(yaml).some((item) => item.level === "error")).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects dangerous YAML keys and alias expansion", () => {
|
||||
expect(() =>
|
||||
parseApiDocument("__proto__: { polluted: true }", "bad.yaml"),
|
||||
).toThrow(/prohibited key/iu);
|
||||
const aliases = `x: &x [1, 2]\ny: [${Array.from({ length: 60 }, () => "*x").join(",")}]`;
|
||||
expect(() => parseApiDocument(aliases, "aliases.yaml")).toThrow(/alias/iu);
|
||||
});
|
||||
|
||||
it("resolves supplied relative files and refuses remote references", () => {
|
||||
const entry = parseApiDocument(
|
||||
"openapi: 3.1.0\ninfo: {title: A, version: '1'}\npaths: {}\ncomponents: { schemas: { Pet: { $ref: 'schemas.yaml#/Pet' } } }",
|
||||
"openapi.yaml",
|
||||
);
|
||||
const schemas = parseApiDocument(
|
||||
"Pet: { type: object, properties: { name: { type: string } } }",
|
||||
"schemas.yaml",
|
||||
);
|
||||
const workspace = createWorkspace(entry, [schemas]);
|
||||
expect(
|
||||
resolveLocalReference(workspace, "schemas.yaml#/Pet").value,
|
||||
).toMatchObject({ type: "object" });
|
||||
expect(() =>
|
||||
resolveLocalReference(workspace, "https://example.test/schema.json"),
|
||||
).toThrow(/disabled/iu);
|
||||
expect(
|
||||
validateOpenApi(
|
||||
parseApiDocument(
|
||||
"openapi: 3.1.0\ninfo: {title: A, version: '1'}\npaths: {}\nx: {$ref: 'https://bad.test/x'}",
|
||||
),
|
||||
),
|
||||
).toContainEqual(expect.objectContaining({ level: "error" }));
|
||||
});
|
||||
|
||||
it("generates bounded samples and reports reference cycles", () => {
|
||||
const entry = parseApiDocument(
|
||||
'{"openapi":"3.1.0","info":{"title":"A","version":"1"},"paths":{},"components":{"schemas":{"Node":{"type":"object","properties":{"next":{"$ref":"#/components/schemas/Node"}}}}}}',
|
||||
"openapi.json",
|
||||
);
|
||||
const workspace = createWorkspace(entry);
|
||||
const schema = resolveLocalReference(
|
||||
workspace,
|
||||
"#/components/schemas/Node",
|
||||
).value;
|
||||
const sample = generateSchemaSample(workspace, schema);
|
||||
expect(sample.value).toMatchObject({
|
||||
next: { next: { $cycle: "openapi.json#/components/schemas/Node" } },
|
||||
});
|
||||
expect(sample.notices[0]).toMatch(/cycle/iu);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user