mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-08-13 22:37:29 +00:00
Add contract and Playwright PR checks
This commit is contained in:
parent
447974dbba
commit
254233c3ab
11 changed files with 277 additions and 45 deletions
|
|
@ -6,7 +6,9 @@
|
|||
"main": "./dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"dev": "tsx src/index.ts"
|
||||
"dev": "tsx src/index.ts",
|
||||
"test": "vitest run src",
|
||||
"test:contract": "vitest run src/contract.test.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@logicsrc/plugin-core": "file:../../packages/plugin-core",
|
||||
|
|
@ -16,6 +18,7 @@
|
|||
"@logicsrc/validators": "file:../../packages/validators"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsx": "^4.21.0"
|
||||
"tsx": "^4.21.0",
|
||||
"vitest": "^4.0.8"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
88
apps/commandboard-api/src/contract.test.ts
Normal file
88
apps/commandboard-api/src/contract.test.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import type { Server } from "node:http";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { createCommandBoardServer } from "./index.js";
|
||||
|
||||
let server: Server;
|
||||
let baseUrl: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
server = createCommandBoardServer();
|
||||
await new Promise<void>((resolve) => server.listen(0, resolve));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") {
|
||||
throw new Error("Expected API server to bind to a local port");
|
||||
}
|
||||
baseUrl = `http://127.0.0.1:${address.port}`;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
});
|
||||
});
|
||||
|
||||
describe("CommandBoard API contracts", () => {
|
||||
it("exposes health contract", async () => {
|
||||
const response = await fetch(`${baseUrl}/health`);
|
||||
const body = await response.json() as { ok: boolean; service: string };
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(body).toEqual({ ok: true, service: "commandboard-api" });
|
||||
});
|
||||
|
||||
it("exposes default plugin contract including sh1pt", async () => {
|
||||
const response = await fetch(`${baseUrl}/api/plugins`);
|
||||
const body = await response.json() as {
|
||||
plugins: Array<{ id: string; enabled: boolean; capabilities: string[] }>;
|
||||
capabilities: Record<string, string[]>;
|
||||
};
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(body.plugins.map((plugin) => plugin.id)).toEqual(["coinpay", "ugig", "sh1pt"]);
|
||||
expect(body.plugins.find((plugin) => plugin.id === "sh1pt")).toMatchObject({
|
||||
enabled: true,
|
||||
capabilities: expect.arrayContaining(["projects.sync", "actions.publish", "deployments.status"])
|
||||
});
|
||||
expect(body.capabilities["actions.publish"]).toEqual(["sh1pt"]);
|
||||
});
|
||||
|
||||
it("exposes sh1pt project and action contracts", async () => {
|
||||
const projectsResponse = await fetch(`${baseUrl}/api/plugins/sh1pt/projects`);
|
||||
const projectsBody = await projectsResponse.json() as { projects: Array<{ id: string; board: string; status: string; actions: number }> };
|
||||
const actionsResponse = await fetch(`${baseUrl}/api/plugins/sh1pt/actions`);
|
||||
const actionsBody = await actionsResponse.json() as { actions: Array<{ id: string; title: string; publishable: boolean }> };
|
||||
|
||||
expect(projectsResponse.status).toBe(200);
|
||||
expect(projectsBody.projects[0]).toMatchObject({ id: "sh1pt_project_1", board: "/projects/sh1pt", status: "active" });
|
||||
expect(actionsResponse.status).toBe(200);
|
||||
expect(actionsBody.actions[0]).toMatchObject({ id: "action_release_checklist", publishable: true });
|
||||
});
|
||||
|
||||
it("accepts sh1pt action publish requests", async () => {
|
||||
const response = await fetch(`${baseUrl}/api/plugins/sh1pt/actions/publish`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ action_id: "action_release_checklist" })
|
||||
});
|
||||
const body = await response.json() as { accepted: boolean; action_id: string; board: string };
|
||||
|
||||
expect(response.status).toBe(202);
|
||||
expect(body).toEqual({
|
||||
accepted: true,
|
||||
action_id: "action_release_checklist",
|
||||
board: "/projects/sh1pt"
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects invalid LogicSRC task payloads", async () => {
|
||||
const response = await fetch(`${baseUrl}/api/tasks`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ type: "logicsrc.task", title: "Incomplete" })
|
||||
});
|
||||
const body = await response.json() as { errors: unknown[] };
|
||||
|
||||
expect(response.status).toBe(422);
|
||||
expect(Array.isArray(body.errors)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { createPluginRegistry } from "@logicsrc/plugin-core";
|
||||
import { coinPayPlugin } from "@logicsrc/plugin-coinpay";
|
||||
import { sh1ptPlugin } from "@logicsrc/plugin-sh1pt";
|
||||
|
|
@ -40,13 +41,15 @@ const sh1ptActions = [
|
|||
{ id: "action_deploy_preview", title: "Deploy preview", publishable: true }
|
||||
];
|
||||
|
||||
const server = createServer(async (request, response) => {
|
||||
try {
|
||||
await route(request, response);
|
||||
} catch (error) {
|
||||
json(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
export function createCommandBoardServer() {
|
||||
return createServer(async (request, response) => {
|
||||
try {
|
||||
await route(request, response);
|
||||
} catch (error) {
|
||||
json(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
const url = new URL(request.url ?? "/", "http://localhost");
|
||||
|
|
@ -141,7 +144,14 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
const port = Number(process.env.PORT ?? 4010);
|
||||
server.listen(port, () => {
|
||||
console.log(`CommandBoard.run API listening on http://localhost:${port}`);
|
||||
});
|
||||
export function startCommandBoardServer(port = Number(process.env.PORT ?? 4010)) {
|
||||
const server = createCommandBoardServer();
|
||||
server.listen(port, () => {
|
||||
console.log(`CommandBoard.run API listening on http://localhost:${port}`);
|
||||
});
|
||||
return server;
|
||||
}
|
||||
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
startCommandBoardServer();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue