chore: auto-fix Biome formatting and configure lint rules
All checks were successful
CI / ci (push) Successful in 15s
All checks were successful
CI / ci (push) Successful in 15s
Run biome check --write --unsafe to fix tabs, import ordering, and non-null assertions across entire codebase. Disable a11y rules not applicable to this single-user app. Exclude auto-generated routeTree. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,91 +1,91 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { categoryRoutes } from "../../src/server/routes/categories.ts";
|
||||
import { itemRoutes } from "../../src/server/routes/items.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
|
||||
app.route("/api/categories", categoryRoutes);
|
||||
app.route("/api/items", itemRoutes);
|
||||
return { app, db };
|
||||
app.route("/api/categories", categoryRoutes);
|
||||
app.route("/api/items", itemRoutes);
|
||||
return { app, db };
|
||||
}
|
||||
|
||||
describe("Category Routes", () => {
|
||||
let app: Hono;
|
||||
let app: Hono;
|
||||
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
|
||||
it("POST /api/categories creates category", async () => {
|
||||
const res = await app.request("/api/categories", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
|
||||
});
|
||||
it("POST /api/categories creates category", async () => {
|
||||
const res = await app.request("/api/categories", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Shelter");
|
||||
expect(body.icon).toBe("tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Shelter");
|
||||
expect(body.icon).toBe("tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("GET /api/categories returns all categories", async () => {
|
||||
const res = await app.request("/api/categories");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
// At minimum, Uncategorized is seeded
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
it("GET /api/categories returns all categories", async () => {
|
||||
const res = await app.request("/api/categories");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
// At minimum, Uncategorized is seeded
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it("DELETE /api/categories/:id reassigns items", async () => {
|
||||
// Create category
|
||||
const catRes = await app.request("/api/categories", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
|
||||
});
|
||||
const cat = await catRes.json();
|
||||
it("DELETE /api/categories/:id reassigns items", async () => {
|
||||
// Create category
|
||||
const catRes = await app.request("/api/categories", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Shelter", icon: "tent" }),
|
||||
});
|
||||
const cat = await catRes.json();
|
||||
|
||||
// Create item in that category
|
||||
await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: cat.id }),
|
||||
});
|
||||
// Create item in that category
|
||||
await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: cat.id }),
|
||||
});
|
||||
|
||||
// Delete the category
|
||||
const delRes = await app.request(`/api/categories/${cat.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
expect(delRes.status).toBe(200);
|
||||
// Delete the category
|
||||
const delRes = await app.request(`/api/categories/${cat.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
expect(delRes.status).toBe(200);
|
||||
|
||||
// Verify items are now in Uncategorized
|
||||
const itemsRes = await app.request("/api/items");
|
||||
const items = await itemsRes.json();
|
||||
const tent = items.find((i: any) => i.name === "Tent");
|
||||
expect(tent.categoryId).toBe(1);
|
||||
});
|
||||
// Verify items are now in Uncategorized
|
||||
const itemsRes = await app.request("/api/items");
|
||||
const items = await itemsRes.json();
|
||||
const tent = items.find((i: any) => i.name === "Tent");
|
||||
expect(tent.categoryId).toBe(1);
|
||||
});
|
||||
|
||||
it("DELETE /api/categories/1 returns 400 (cannot delete Uncategorized)", async () => {
|
||||
const res = await app.request("/api/categories/1", {
|
||||
method: "DELETE",
|
||||
});
|
||||
it("DELETE /api/categories/1 returns 400 (cannot delete Uncategorized)", async () => {
|
||||
const res = await app.request("/api/categories/1", {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toContain("Uncategorized");
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
const body = await res.json();
|
||||
expect(body.error).toContain("Uncategorized");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,121 +1,121 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { itemRoutes } from "../../src/server/routes/items.ts";
|
||||
import { categoryRoutes } from "../../src/server/routes/categories.ts";
|
||||
import { itemRoutes } from "../../src/server/routes/items.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
|
||||
app.route("/api/items", itemRoutes);
|
||||
app.route("/api/categories", categoryRoutes);
|
||||
return { app, db };
|
||||
app.route("/api/items", itemRoutes);
|
||||
app.route("/api/categories", categoryRoutes);
|
||||
return { app, db };
|
||||
}
|
||||
|
||||
describe("Item Routes", () => {
|
||||
let app: Hono;
|
||||
let app: Hono;
|
||||
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
|
||||
it("POST /api/items with valid data returns 201", async () => {
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
}),
|
||||
});
|
||||
it("POST /api/items with valid data returns 201", async () => {
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
priceCents: 35000,
|
||||
categoryId: 1,
|
||||
}),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("POST /api/items with missing name returns 400", async () => {
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ categoryId: 1 }),
|
||||
});
|
||||
it("POST /api/items with missing name returns 400", async () => {
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ categoryId: 1 }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("GET /api/items returns array", async () => {
|
||||
// Create an item first
|
||||
await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
|
||||
});
|
||||
it("GET /api/items returns array", async () => {
|
||||
// Create an item first
|
||||
await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
|
||||
});
|
||||
|
||||
const res = await app.request("/api/items");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
const res = await app.request("/api/items");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it("PUT /api/items/:id updates fields", async () => {
|
||||
// Create first
|
||||
const createRes = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
}),
|
||||
});
|
||||
const created = await createRes.json();
|
||||
it("PUT /api/items/:id updates fields", async () => {
|
||||
// Create first
|
||||
const createRes = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Tent",
|
||||
weightGrams: 1200,
|
||||
categoryId: 1,
|
||||
}),
|
||||
});
|
||||
const created = await createRes.json();
|
||||
|
||||
// Update
|
||||
const res = await app.request(`/api/items/${created.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Big Agnes Tent", weightGrams: 1100 }),
|
||||
});
|
||||
// Update
|
||||
const res = await app.request(`/api/items/${created.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Big Agnes Tent", weightGrams: 1100 }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Big Agnes Tent");
|
||||
expect(body.weightGrams).toBe(1100);
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Big Agnes Tent");
|
||||
expect(body.weightGrams).toBe(1100);
|
||||
});
|
||||
|
||||
it("DELETE /api/items/:id returns success", async () => {
|
||||
// Create first
|
||||
const createRes = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
|
||||
});
|
||||
const created = await createRes.json();
|
||||
it("DELETE /api/items/:id returns success", async () => {
|
||||
// Create first
|
||||
const createRes = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Tent", categoryId: 1 }),
|
||||
});
|
||||
const created = await createRes.json();
|
||||
|
||||
const res = await app.request(`/api/items/${created.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const res = await app.request(`/api/items/${created.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
});
|
||||
|
||||
it("GET /api/items/:id returns 404 for non-existent item", async () => {
|
||||
const res = await app.request("/api/items/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
it("GET /api/items/:id returns 404 for non-existent item", async () => {
|
||||
const res = await app.request("/api/items/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,229 +1,244 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { setupRoutes } from "../../src/server/routes/setups.ts";
|
||||
import { itemRoutes } from "../../src/server/routes/items.ts";
|
||||
import { setupRoutes } from "../../src/server/routes/setups.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
|
||||
app.route("/api/setups", setupRoutes);
|
||||
app.route("/api/items", itemRoutes);
|
||||
return { app, db };
|
||||
app.route("/api/setups", setupRoutes);
|
||||
app.route("/api/items", itemRoutes);
|
||||
return { app, db };
|
||||
}
|
||||
|
||||
async function createSetupViaAPI(app: Hono, name: string) {
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
return res.json();
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function createItemViaAPI(app: Hono, data: any) {
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.json();
|
||||
const res = await app.request("/api/items", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
describe("Setup Routes", () => {
|
||||
let app: Hono;
|
||||
let app: Hono;
|
||||
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
|
||||
describe("POST /api/setups", () => {
|
||||
it("with valid body returns 201 + setup object", async () => {
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Day Hike" }),
|
||||
});
|
||||
describe("POST /api/setups", () => {
|
||||
it("with valid body returns 201 + setup object", async () => {
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Day Hike" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Day Hike");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Day Hike");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("with empty name returns 400", async () => {
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "" }),
|
||||
});
|
||||
it("with empty name returns 400", async () => {
|
||||
const res = await app.request("/api/setups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/setups", () => {
|
||||
it("returns array of setups with totals", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Backpacking");
|
||||
const item = await createItemViaAPI(app, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
describe("GET /api/setups", () => {
|
||||
it("returns array of setups with totals", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Backpacking");
|
||||
const item = await createItemViaAPI(app, {
|
||||
name: "Tent",
|
||||
categoryId: 1,
|
||||
weightGrams: 1200,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
// Sync items
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item.id] }),
|
||||
});
|
||||
// Sync items
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item.id] }),
|
||||
});
|
||||
|
||||
const res = await app.request("/api/setups");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
expect(body[0].itemCount).toBeDefined();
|
||||
expect(body[0].totalWeight).toBeDefined();
|
||||
expect(body[0].totalCost).toBeDefined();
|
||||
});
|
||||
});
|
||||
const res = await app.request("/api/setups");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
expect(body[0].itemCount).toBeDefined();
|
||||
expect(body[0].totalWeight).toBeDefined();
|
||||
expect(body[0].totalCost).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/setups/:id", () => {
|
||||
it("returns setup with items", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Day Hike");
|
||||
const item = await createItemViaAPI(app, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
describe("GET /api/setups/:id", () => {
|
||||
it("returns setup with items", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Day Hike");
|
||||
const item = await createItemViaAPI(app, {
|
||||
name: "Water Bottle",
|
||||
categoryId: 1,
|
||||
weightGrams: 200,
|
||||
priceCents: 2500,
|
||||
});
|
||||
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item.id] }),
|
||||
});
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item.id] }),
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/setups/${setup.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Day Hike");
|
||||
expect(body.items).toHaveLength(1);
|
||||
expect(body.items[0].name).toBe("Water Bottle");
|
||||
});
|
||||
const res = await app.request(`/api/setups/${setup.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Day Hike");
|
||||
expect(body.items).toHaveLength(1);
|
||||
expect(body.items[0].name).toBe("Water Bottle");
|
||||
});
|
||||
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/setups/:id", () => {
|
||||
it("updates setup name", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Original");
|
||||
describe("PUT /api/setups/:id", () => {
|
||||
it("updates setup name", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Original");
|
||||
|
||||
const res = await app.request(`/api/setups/${setup.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Renamed" }),
|
||||
});
|
||||
const res = await app.request(`/api/setups/${setup.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Renamed" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Renamed");
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Renamed");
|
||||
});
|
||||
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Ghost" }),
|
||||
});
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Ghost" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/setups/:id", () => {
|
||||
it("removes setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "To Delete");
|
||||
describe("DELETE /api/setups/:id", () => {
|
||||
it("removes setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "To Delete");
|
||||
|
||||
const res = await app.request(`/api/setups/${setup.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const res = await app.request(`/api/setups/${setup.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
|
||||
// Verify gone
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
expect(getRes.status).toBe(404);
|
||||
});
|
||||
// Verify gone
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
expect(getRes.status).toBe(404);
|
||||
});
|
||||
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999", { method: "DELETE" });
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
it("returns 404 for non-existent setup", async () => {
|
||||
const res = await app.request("/api/setups/9999", { method: "DELETE" });
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/setups/:id/items", () => {
|
||||
it("syncs items to setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Kit");
|
||||
const item1 = await createItemViaAPI(app, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = await createItemViaAPI(app, { name: "Item 2", categoryId: 1 });
|
||||
describe("PUT /api/setups/:id/items", () => {
|
||||
it("syncs items to setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Kit");
|
||||
const item1 = await createItemViaAPI(app, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItemViaAPI(app, {
|
||||
name: "Item 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
|
||||
});
|
||||
const res = await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
|
||||
// Verify items
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
const getBody = await getRes.json();
|
||||
expect(getBody.items).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
// Verify items
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
const getBody = await getRes.json();
|
||||
expect(getBody.items).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/setups/:id/items/:itemId", () => {
|
||||
it("removes single item from setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Kit");
|
||||
const item1 = await createItemViaAPI(app, { name: "Item 1", categoryId: 1 });
|
||||
const item2 = await createItemViaAPI(app, { name: "Item 2", categoryId: 1 });
|
||||
describe("DELETE /api/setups/:id/items/:itemId", () => {
|
||||
it("removes single item from setup", async () => {
|
||||
const setup = await createSetupViaAPI(app, "Kit");
|
||||
const item1 = await createItemViaAPI(app, {
|
||||
name: "Item 1",
|
||||
categoryId: 1,
|
||||
});
|
||||
const item2 = await createItemViaAPI(app, {
|
||||
name: "Item 2",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Sync both items
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
|
||||
});
|
||||
// Sync both items
|
||||
await app.request(`/api/setups/${setup.id}/items`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ itemIds: [item1.id, item2.id] }),
|
||||
});
|
||||
|
||||
// Remove one
|
||||
const res = await app.request(`/api/setups/${setup.id}/items/${item1.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
// Remove one
|
||||
const res = await app.request(
|
||||
`/api/setups/${setup.id}/items/${item1.id}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
// Verify only one remains
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
const getBody = await getRes.json();
|
||||
expect(getBody.items).toHaveLength(1);
|
||||
expect(getBody.items[0].name).toBe("Item 2");
|
||||
});
|
||||
});
|
||||
// Verify only one remains
|
||||
const getRes = await app.request(`/api/setups/${setup.id}`);
|
||||
const getBody = await getRes.json();
|
||||
expect(getBody.items).toHaveLength(1);
|
||||
expect(getBody.items[0].name).toBe("Item 2");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,300 +1,300 @@
|
||||
import { describe, it, expect, beforeEach } from "bun:test";
|
||||
import { beforeEach, describe, expect, it } from "bun:test";
|
||||
import { Hono } from "hono";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
import { threadRoutes } from "../../src/server/routes/threads.ts";
|
||||
import { createTestDb } from "../helpers/db.ts";
|
||||
|
||||
function createTestApp() {
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
const db = createTestDb();
|
||||
const app = new Hono();
|
||||
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
// Inject test DB into context for all routes
|
||||
app.use("*", async (c, next) => {
|
||||
c.set("db", db);
|
||||
await next();
|
||||
});
|
||||
|
||||
app.route("/api/threads", threadRoutes);
|
||||
return { app, db };
|
||||
app.route("/api/threads", threadRoutes);
|
||||
return { app, db };
|
||||
}
|
||||
|
||||
async function createThreadViaAPI(app: Hono, name: string, categoryId = 1) {
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, categoryId }),
|
||||
});
|
||||
return res.json();
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, categoryId }),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function createCandidateViaAPI(app: Hono, threadId: number, data: any) {
|
||||
const res = await app.request(`/api/threads/${threadId}/candidates`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.json();
|
||||
const res = await app.request(`/api/threads/${threadId}/candidates`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
describe("Thread Routes", () => {
|
||||
let app: Hono;
|
||||
let app: Hono;
|
||||
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
beforeEach(() => {
|
||||
const testApp = createTestApp();
|
||||
app = testApp.app;
|
||||
});
|
||||
|
||||
describe("POST /api/threads", () => {
|
||||
it("with valid body returns 201 + thread object", async () => {
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "New Tent", categoryId: 1 }),
|
||||
});
|
||||
describe("POST /api/threads", () => {
|
||||
it("with valid body returns 201 + thread object", async () => {
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "New Tent", categoryId: 1 }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("New Tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
expect(body.status).toBe("active");
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("New Tent");
|
||||
expect(body.id).toBeGreaterThan(0);
|
||||
expect(body.status).toBe("active");
|
||||
});
|
||||
|
||||
it("with empty name returns 400", async () => {
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "" }),
|
||||
});
|
||||
it("with empty name returns 400", async () => {
|
||||
const res = await app.request("/api/threads", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/threads", () => {
|
||||
it("returns array of active threads with metadata", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Backpack Options");
|
||||
await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Pack A",
|
||||
categoryId: 1,
|
||||
priceCents: 20000,
|
||||
});
|
||||
describe("GET /api/threads", () => {
|
||||
it("returns array of active threads with metadata", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Backpack Options");
|
||||
await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Pack A",
|
||||
categoryId: 1,
|
||||
priceCents: 20000,
|
||||
});
|
||||
|
||||
const res = await app.request("/api/threads");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
expect(body[0].candidateCount).toBeDefined();
|
||||
});
|
||||
const res = await app.request("/api/threads");
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body.length).toBeGreaterThanOrEqual(1);
|
||||
expect(body[0].candidateCount).toBeDefined();
|
||||
});
|
||||
|
||||
it("?includeResolved=true includes archived threads", async () => {
|
||||
const t1 = await createThreadViaAPI(app, "Active");
|
||||
const t2 = await createThreadViaAPI(app, "To Resolve");
|
||||
const candidate = await createCandidateViaAPI(app, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
it("?includeResolved=true includes archived threads", async () => {
|
||||
const _t1 = await createThreadViaAPI(app, "Active");
|
||||
const t2 = await createThreadViaAPI(app, "To Resolve");
|
||||
const candidate = await createCandidateViaAPI(app, t2.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Resolve thread
|
||||
await app.request(`/api/threads/${t2.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
// Resolve thread
|
||||
await app.request(`/api/threads/${t2.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
|
||||
// Default excludes resolved
|
||||
const defaultRes = await app.request("/api/threads");
|
||||
const defaultBody = await defaultRes.json();
|
||||
expect(defaultBody).toHaveLength(1);
|
||||
// Default excludes resolved
|
||||
const defaultRes = await app.request("/api/threads");
|
||||
const defaultBody = await defaultRes.json();
|
||||
expect(defaultBody).toHaveLength(1);
|
||||
|
||||
// With includeResolved includes all
|
||||
const allRes = await app.request("/api/threads?includeResolved=true");
|
||||
const allBody = await allRes.json();
|
||||
expect(allBody).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
// With includeResolved includes all
|
||||
const allRes = await app.request("/api/threads?includeResolved=true");
|
||||
const allBody = await allRes.json();
|
||||
expect(allBody).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/threads/:id", () => {
|
||||
it("returns thread with candidates", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Tent Options");
|
||||
await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
priceCents: 30000,
|
||||
});
|
||||
describe("GET /api/threads/:id", () => {
|
||||
it("returns thread with candidates", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Tent Options");
|
||||
await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Tent A",
|
||||
categoryId: 1,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/threads/${thread.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Tent Options");
|
||||
expect(body.candidates).toHaveLength(1);
|
||||
expect(body.candidates[0].name).toBe("Tent A");
|
||||
});
|
||||
const res = await app.request(`/api/threads/${thread.id}`);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Tent Options");
|
||||
expect(body.candidates).toHaveLength(1);
|
||||
expect(body.candidates[0].name).toBe("Tent A");
|
||||
});
|
||||
|
||||
it("returns 404 for non-existent thread", async () => {
|
||||
const res = await app.request("/api/threads/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
it("returns 404 for non-existent thread", async () => {
|
||||
const res = await app.request("/api/threads/9999");
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/threads/:id", () => {
|
||||
it("updates thread name", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Original");
|
||||
describe("PUT /api/threads/:id", () => {
|
||||
it("updates thread name", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Original");
|
||||
|
||||
const res = await app.request(`/api/threads/${thread.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Renamed" }),
|
||||
});
|
||||
const res = await app.request(`/api/threads/${thread.id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Renamed" }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Renamed");
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Renamed");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/threads/:id", () => {
|
||||
it("removes thread", async () => {
|
||||
const thread = await createThreadViaAPI(app, "To Delete");
|
||||
describe("DELETE /api/threads/:id", () => {
|
||||
it("removes thread", async () => {
|
||||
const thread = await createThreadViaAPI(app, "To Delete");
|
||||
|
||||
const res = await app.request(`/api/threads/${thread.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const res = await app.request(`/api/threads/${thread.id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
|
||||
// Verify gone
|
||||
const getRes = await app.request(`/api/threads/${thread.id}`);
|
||||
expect(getRes.status).toBe(404);
|
||||
});
|
||||
});
|
||||
// Verify gone
|
||||
const getRes = await app.request(`/api/threads/${thread.id}`);
|
||||
expect(getRes.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/threads/:id/candidates", () => {
|
||||
it("adds candidate, returns 201", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
describe("POST /api/threads/:id/candidates", () => {
|
||||
it("adds candidate, returns 201", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
|
||||
const res = await app.request(`/api/threads/${thread.id}/candidates`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Candidate A",
|
||||
categoryId: 1,
|
||||
priceCents: 25000,
|
||||
weightGrams: 500,
|
||||
}),
|
||||
});
|
||||
const res = await app.request(`/api/threads/${thread.id}/candidates`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: "Candidate A",
|
||||
categoryId: 1,
|
||||
priceCents: 25000,
|
||||
weightGrams: 500,
|
||||
}),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Candidate A");
|
||||
expect(body.threadId).toBe(thread.id);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(201);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Candidate A");
|
||||
expect(body.threadId).toBe(thread.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/threads/:threadId/candidates/:candidateId", () => {
|
||||
it("updates candidate", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
describe("PUT /api/threads/:threadId/candidates/:candidateId", () => {
|
||||
it("updates candidate", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Original",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const res = await app.request(
|
||||
`/api/threads/${thread.id}/candidates/${candidate.id}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Updated" }),
|
||||
},
|
||||
);
|
||||
const res = await app.request(
|
||||
`/api/threads/${thread.id}/candidates/${candidate.id}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: "Updated" }),
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Updated");
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.name).toBe("Updated");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/threads/:threadId/candidates/:candidateId", () => {
|
||||
it("removes candidate", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "To Remove",
|
||||
categoryId: 1,
|
||||
});
|
||||
describe("DELETE /api/threads/:threadId/candidates/:candidateId", () => {
|
||||
it("removes candidate", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Test");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "To Remove",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const res = await app.request(
|
||||
`/api/threads/${thread.id}/candidates/${candidate.id}`,
|
||||
{ method: "DELETE" },
|
||||
);
|
||||
const res = await app.request(
|
||||
`/api/threads/${thread.id}/candidates/${candidate.id}`,
|
||||
{ method: "DELETE" },
|
||||
);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/threads/:id/resolve", () => {
|
||||
it("with valid candidateId returns 200 + created item", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Tent Decision");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
priceCents: 30000,
|
||||
});
|
||||
describe("POST /api/threads/:id/resolve", () => {
|
||||
it("with valid candidateId returns 200 + created item", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Tent Decision");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
priceCents: 30000,
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.item).toBeDefined();
|
||||
expect(body.item.name).toBe("Winner");
|
||||
expect(body.item.priceCents).toBe(30000);
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.item).toBeDefined();
|
||||
expect(body.item.name).toBe("Winner");
|
||||
expect(body.item.priceCents).toBe(30000);
|
||||
});
|
||||
|
||||
it("on already-resolved thread returns 400", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Already Resolved");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
it("on already-resolved thread returns 400", async () => {
|
||||
const thread = await createThreadViaAPI(app, "Already Resolved");
|
||||
const candidate = await createCandidateViaAPI(app, thread.id, {
|
||||
name: "Winner",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
// Resolve first time
|
||||
await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
// Resolve first time
|
||||
await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
|
||||
// Try again
|
||||
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
// Try again
|
||||
const res = await app.request(`/api/threads/${thread.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("with wrong candidateId returns 400", async () => {
|
||||
const t1 = await createThreadViaAPI(app, "Thread 1");
|
||||
const t2 = await createThreadViaAPI(app, "Thread 2");
|
||||
const candidate = await createCandidateViaAPI(app, t2.id, {
|
||||
name: "Wrong Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
it("with wrong candidateId returns 400", async () => {
|
||||
const t1 = await createThreadViaAPI(app, "Thread 1");
|
||||
const t2 = await createThreadViaAPI(app, "Thread 2");
|
||||
const candidate = await createCandidateViaAPI(app, t2.id, {
|
||||
name: "Wrong Thread",
|
||||
categoryId: 1,
|
||||
});
|
||||
|
||||
const res = await app.request(`/api/threads/${t1.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
const res = await app.request(`/api/threads/${t1.id}/resolve`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ candidateId: candidate.id }),
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user