diff --git a/.github/workflows/server-tests.yml b/.github/workflows/server-tests.yml new file mode 100644 index 00000000..29c8b1f4 --- /dev/null +++ b/.github/workflows/server-tests.yml @@ -0,0 +1,31 @@ +name: Dev Server API Tests + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup bun + uses: oven-sh/setup-bun@v1 + with: + bun-version: latest + + - name: Install dependencies + run: bun run bootstrap + + - name: Install dependencies + run: bun run build + + - name: Run tests + run: cd dev-server-api && bun test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f13e67fa..05bf0173 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,9 @@ -name: Bun Test +name: CLI Tests on: + push: + branches: + - main pull_request: branches: - main diff --git a/.gitignore b/.gitignore index f9417b1b..e213d95d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ dist *.__tmp_entrypoint.tsx *.zip -.vscode \ No newline at end of file +.vscode +.aider* diff --git a/dev-server-api/bun.lockb b/dev-server-api/bun.lockb index 6929ca22..d694e621 100755 Binary files a/dev-server-api/bun.lockb and b/dev-server-api/bun.lockb differ diff --git a/dev-server-api/package.json b/dev-server-api/package.json index c515d3a6..f5cb3674 100644 --- a/dev-server-api/package.json +++ b/dev-server-api/package.json @@ -19,6 +19,7 @@ "better-sqlite3": "^11.0.0", "kysely": "^0.27.3", "kysely-bun-sqlite": "^0.3.2", + "redaxios": "^0.5.1", "winterspec": "0.0.81", "zod": "^3.22.4" } diff --git a/dev-server-api/tests/fixtures/get-test-server.ts b/dev-server-api/tests/fixtures/get-test-server.ts new file mode 100644 index 00000000..41870d61 --- /dev/null +++ b/dev-server-api/tests/fixtures/get-test-server.ts @@ -0,0 +1,29 @@ +import { afterEach } from "bun:test" +import defaultAxios from "redaxios" +import { startServer } from "./start-server" + +interface TestFixture { + url: string + server: any + axios: typeof defaultAxios +} + +export const getTestFixture = async (): Promise => { + const port = 3001 + Math.floor(Math.random() * 999) + const server = startServer({ port }) + const url = `http://localhost:${port}` + const axios = defaultAxios.create({ + baseURL: url, + }) + + afterEach(() => { + console.log("closing server") + server.stop() + }) + + return { + url, + server, + axios, + } +} diff --git a/dev-server-api/tests/fixtures/start-server.ts b/dev-server-api/tests/fixtures/start-server.ts new file mode 100644 index 00000000..c3bca20b --- /dev/null +++ b/dev-server-api/tests/fixtures/start-server.ts @@ -0,0 +1,20 @@ +import { createFetchHandlerFromDir } from "winterspec/adapters/node" +import { Request as EdgeRuntimeRequest } from "@edge-runtime/primitives" +import { join } from "node:path" + +const serverFetch = await createFetchHandlerFromDir( + join(import.meta.dir, "../../routes") +) + +export const startServer = ({ port }: { port: number }) => + Bun.serve({ + fetch: (bunReq) => { + const req = new EdgeRuntimeRequest(bunReq.url, { + headers: bunReq.headers, + method: bunReq.method, + body: bunReq.body, + }) + return serverFetch(req as any) + }, + port, + }) diff --git a/dev-server-api/tests/routes/health.test.ts b/dev-server-api/tests/routes/health.test.ts new file mode 100644 index 00000000..10295ba4 --- /dev/null +++ b/dev-server-api/tests/routes/health.test.ts @@ -0,0 +1,10 @@ +import { it, expect } from "bun:test" +import { getTestFixture } from "../fixtures/get-test-server" + +it("GET /health", async () => { + const { axios } = await getTestFixture() + + expect(await axios.get("/health").then((r) => r.data)).toMatchObject({ + ok: true, + }) +})