Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ If you've received this link for a technical interview, follow these steps to se
### Quick Setup (<5 minutes)

1. **Prerequisites**
- Node.js (v14 or higher) - [Download here](https://nodejs.org/)
- npm (v6 or higher) - comes with Node.js
- Node.js 18.12 or newer (Node.js 22 is recommended) - [Download here](https://nodejs.org/)
- npm 9 or newer - comes with supported Node.js releases

2. **Installation**
```bash
# Clone or extract the project to your local machine
# Open a terminal in the project directory

# Install dependencies (this may take a minute)
npm install
npm ci

Copy link
Copy Markdown

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 ci was for... CI? 😂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✌️ clean install ✌️

Copy link
Copy Markdown

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. 🤣


# Start the development server
npm start
Expand Down Expand Up @@ -102,7 +102,7 @@ git clone git@github.com:webflow/codeflow.git
cd codeflow

# Install dependencies
npm install
npm ci

# Start development server
npm start
Expand Down Expand Up @@ -134,4 +134,4 @@ This project is licensed under the Apache License 2.0 - see the [LICENSE](./LICE

For issues or questions:
- **Candidates**: Contact your interviewer or recruiting team
- **Contributors**: Open an issue on GitHub or check existing discussions
- **Contributors**: Open an issue on GitHub or check existing discussions
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"main": "index.js",
"private": true,
"author": "Kaden Beckstead",
"engines": {
"node": ">=18.12.0",
"npm": ">=9.0.0"
},
"dependencies": {
"@radix-ui/react-dialog": "^1.1.15",
"@radix-ui/react-dropdown-menu": "^2.1.16",
Expand Down
43 changes: 43 additions & 0 deletions src/server/concurrency.test.js
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"]);
});
});
107 changes: 107 additions & 0 deletions src/server/index.test.js
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();
});
});
54 changes: 54 additions & 0 deletions src/server/router.test.js
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);
});
});