Repository navigation
Add infrastructure tests and runtime requirements #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Roach
wants to merge
1
commit into
main
Choose a base branch
from
test/infra-and-runtime-requirements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| engine-strict=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 22 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { createPool } from "./concurrency"; | ||
|
|
||
| describe("mock API concurrency pool", () => { | ||
| test("allows work up to the configured limit", async () => { | ||
| const pool = createPool(2); | ||
|
|
||
| await expect(pool.acquire()).resolves.toBeUndefined(); | ||
| await expect(pool.acquire()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("queues work beyond the limit until a slot is released", async () => { | ||
| const pool = createPool(1); | ||
| await pool.acquire(); | ||
|
|
||
| let acquired = false; | ||
| const waiting = pool.acquire().then(() => { | ||
| acquired = true; | ||
| }); | ||
| await Promise.resolve(); | ||
| expect(acquired).toBe(false); | ||
|
|
||
| pool.release(); | ||
| await waiting; | ||
| expect(acquired).toBe(true); | ||
| }); | ||
|
|
||
| test("releases queued work in FIFO order", async () => { | ||
| const pool = createPool(1); | ||
| await pool.acquire(); | ||
| const order = []; | ||
|
|
||
| const second = pool.acquire().then(() => order.push("second")); | ||
| const third = pool.acquire().then(() => order.push("third")); | ||
|
|
||
| pool.release(); | ||
| await second; | ||
| expect(order).toEqual(["second"]); | ||
|
|
||
| pool.release(); | ||
| await third; | ||
| expect(order).toEqual(["second", "third"]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| jest.mock("@/server/concurrency", () => require("./concurrency"), { virtual: true }); | ||
| jest.mock("@/server/router", () => require("./router"), { virtual: true }); | ||
| jest.mock("@/server/serviceWorkerController", () => ({ | ||
| ensureController: jest.fn().mockResolvedValue(undefined), | ||
| }), { virtual: true }); | ||
|
|
||
| describe("mock API server", () => { | ||
| let messageListener; | ||
| let server; | ||
| let logSpy; | ||
|
|
||
| beforeAll(async () => { | ||
| jest.useFakeTimers(); | ||
| logSpy = jest.spyOn(console, "log").mockImplementation(() => {}); | ||
| Object.defineProperty(navigator, "serviceWorker", { | ||
| configurable: true, | ||
| value: { | ||
| addEventListener: jest.fn((type, listener) => { | ||
| if (type === "message") messageListener = listener; | ||
| }), | ||
| }, | ||
| }); | ||
|
|
||
| server = require("./index"); | ||
| await server.initServer; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| logSpy.mockRestore(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| function request(data) { | ||
| const postMessage = jest.fn(); | ||
| const completion = messageListener({ data, ports: [{ postMessage }] }); | ||
| return { postMessage, completion }; | ||
| } | ||
|
|
||
| test("passes params, query, and body to a matching handler", async () => { | ||
| const handler = jest.fn().mockResolvedValue({ saved: true }); | ||
| await server.setupRoutes([ | ||
| { method: "POST", path: "/api/items/:id", handler }, | ||
| ]); | ||
|
|
||
| const { postMessage, completion } = request({ | ||
| type: "mock-api-request", | ||
| method: "POST", | ||
| url: "/api/items/42?draft=true", | ||
| body: { name: "Example" }, | ||
| }); | ||
| await Promise.resolve(); | ||
| jest.runOnlyPendingTimers(); | ||
| await completion; | ||
|
|
||
| expect(handler).toHaveBeenCalledWith({ | ||
| params: { id: "42" }, | ||
| query: { draft: "true" }, | ||
| body: { name: "Example" }, | ||
| }); | ||
| expect(postMessage).toHaveBeenCalledWith({ status: 200, body: { saved: true } }); | ||
| }); | ||
|
|
||
| test("returns 404 when no route matches", async () => { | ||
| await server.setupRoutes([]); | ||
| const { postMessage, completion } = request({ | ||
| type: "mock-api-request", | ||
| method: "GET", | ||
| url: "/api/missing", | ||
| }); | ||
| await completion; | ||
|
|
||
| expect(postMessage).toHaveBeenCalledWith({ | ||
| status: 404, | ||
| body: { error: "No handler for GET /api/missing" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("returns 500 when a route handler throws", async () => { | ||
| await server.setupRoutes([ | ||
| { | ||
| method: "GET", | ||
| path: "/api/failure", | ||
| handler: jest.fn().mockRejectedValue(new Error("boom")), | ||
| }, | ||
| ]); | ||
| const { postMessage, completion } = request({ | ||
| type: "mock-api-request", | ||
| method: "GET", | ||
| url: "/api/failure", | ||
| }); | ||
| await Promise.resolve(); | ||
| jest.runOnlyPendingTimers(); | ||
| await completion; | ||
|
|
||
| expect(postMessage).toHaveBeenCalledWith({ | ||
| status: 500, | ||
| body: { error: "Error: boom" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("ignores unrelated service worker messages", async () => { | ||
| const postMessage = jest.fn(); | ||
| await messageListener({ data: { type: "other" }, ports: [{ postMessage }] }); | ||
|
|
||
| expect(postMessage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { compilePath, matchRoute } from "./router"; | ||
|
|
||
| const handler = jest.fn(); | ||
|
|
||
| function route(method, path, routeHandler = handler) { | ||
| return { method, regex: compilePath(path), handler: routeHandler }; | ||
| } | ||
|
|
||
| describe("mock API router", () => { | ||
| beforeEach(() => handler.mockReset()); | ||
|
|
||
| test("matches a static route exactly", () => { | ||
| const routes = [route("GET", "/api/items")]; | ||
|
|
||
| expect(matchRoute(routes, "GET", "/api/items")).toEqual({ handler, params: {} }); | ||
| expect(matchRoute(routes, "GET", "/api/items/1")).toBeNull(); | ||
| }); | ||
|
|
||
| test("extracts named path parameters", () => { | ||
| const matched = matchRoute( | ||
| [route("GET", "/api/teams/:teamId/items/:itemId")], | ||
| "GET", | ||
| "/api/teams/platform/items/42" | ||
| ); | ||
|
|
||
| expect(matched).toEqual({ | ||
| handler, | ||
| params: { teamId: "platform", itemId: "42" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("does not match the wrong HTTP method", () => { | ||
| expect(matchRoute([route("POST", "/api/items")], "GET", "/api/items")).toBeNull(); | ||
| }); | ||
|
|
||
| test("treats regex characters in literal segments as plain text", () => { | ||
| const routes = [route("GET", "/api/releases/v1.0+beta")]; | ||
|
|
||
| expect(matchRoute(routes, "GET", "/api/releases/v1.0+beta")).not.toBeNull(); | ||
| expect(matchRoute(routes, "GET", "/api/releases/v1x00beta")).toBeNull(); | ||
| }); | ||
|
|
||
| test("uses the first matching route", () => { | ||
| const first = jest.fn(); | ||
| const second = jest.fn(); | ||
| const matched = matchRoute( | ||
| [route("GET", "/api/items/:id", first), route("GET", "/api/items/:slug", second)], | ||
| "GET", | ||
| "/api/items/42" | ||
| ); | ||
|
|
||
| expect(matched?.handler).toBe(first); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a Node developer, so I will ask a potentially silly question: is this line correct? Should candidates indeed be installing the code in this repo using a command called
npm ci?I would have guessed based on the name that
npm ciwas for... CI? 😂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✌️ clean install ✌️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, you learn something new everyday. 🤣