test(18-02): add failing tests for global item service and seed

- 10 test cases covering search, owner count, link/unlink, seed idempotency
- Added globalItems/itemGlobalLinks tables to SQLite schema
- Added Zod schemas and types for global items
- Created 18-item bikepacking gear seed data JSON
This commit is contained in:
2026-04-05 13:05:28 +02:00
parent f7c9f3dc94
commit 3a6876f7e8
8 changed files with 1393 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
CREATE TABLE `global_items` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`brand` text NOT NULL,
`model` text NOT NULL,
`category` text,
`weight_grams` real,
`price_cents` integer,
`image_url` text,
`description` text,
`created_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `item_global_links` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`item_id` integer NOT NULL,
`global_item_id` integer NOT NULL,
FOREIGN KEY (`item_id`) REFERENCES `items`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`global_item_id`) REFERENCES `global_items`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `item_global_links_item_id_unique` ON `item_global_links` (`item_id`);

File diff suppressed because it is too large Load Diff

View File

@@ -71,6 +71,13 @@
"when": 1775287060443, "when": 1775287060443,
"tag": "0009_happy_mockingbird", "tag": "0009_happy_mockingbird",
"breakpoints": true "breakpoints": true
},
{
"idx": 10,
"version": "6",
"when": 1775387093955,
"tag": "0010_demonic_rawhide_kid",
"breakpoints": true
} }
] ]
} }

View File

@@ -0,0 +1,146 @@
[
{
"brand": "Revelate Designs",
"model": "Terrapin System",
"category": "bags",
"weightGrams": 529,
"priceCents": 18500,
"description": "Waterproof saddle bag with 14L capacity, roll-top closure, and integrated Revelate seat bag mount."
},
{
"brand": "Apidura",
"model": "Expedition Handlebar Pack",
"category": "bags",
"weightGrams": 300,
"priceCents": 16000,
"description": "14L waterproof handlebar roll bag with internal dry bag and accessory pocket."
},
{
"brand": "Ortlieb",
"model": "Frame-Pack Toptube",
"category": "bags",
"weightGrams": 180,
"priceCents": 7500,
"description": "4L waterproof top-tube bag with magnetic closure and reflective details."
},
{
"brand": "Revelate Designs",
"model": "Tangle Frame Bag",
"category": "bags",
"weightGrams": 170,
"priceCents": 13500,
"description": "Full-frame bag with water-resistant construction and multiple internal pockets."
},
{
"brand": "Big Agnes",
"model": "Copper Spur HV UL1",
"category": "shelters",
"weightGrams": 879,
"priceCents": 42000,
"description": "Ultralight 1-person freestanding tent with high-volume hub design and DAC Featherlite poles."
},
{
"brand": "Tarptent",
"model": "Protrail Li",
"category": "shelters",
"weightGrams": 454,
"priceCents": 35000,
"description": "Ultralight single-wall trekking pole shelter in Dyneema composite fabric."
},
{
"brand": "Outdoor Research",
"model": "Helium Bivy",
"category": "shelters",
"weightGrams": 510,
"priceCents": 24900,
"description": "Waterproof breathable bivy sack with single-hoop pole and full-zip entry."
},
{
"brand": "Sea to Summit",
"model": "Spark SP1",
"category": "sleep-systems",
"weightGrams": 375,
"priceCents": 28000,
"description": "Ultralight 850+ fill down sleeping bag rated to 40F/4C with Ultra-Dry Down."
},
{
"brand": "Nemo",
"model": "Tensor Ultralight Insulated Regular",
"category": "sleep-systems",
"weightGrams": 425,
"priceCents": 18000,
"description": "3-inch thick insulated sleeping pad with R-value 4.2 and Spaceframe baffles."
},
{
"brand": "Therm-a-Rest",
"model": "NeoAir XLite NXT",
"category": "sleep-systems",
"weightGrams": 354,
"priceCents": 22000,
"description": "Ultralight insulated air pad with R-value 4.5, Triangular Core Matrix, and WingLock valve."
},
{
"brand": "MSR",
"model": "PocketRocket 2",
"category": "cooking",
"weightGrams": 73,
"priceCents": 5500,
"description": "Ultralight canister stove with adjustable flame control, boils 1L in 3.5 minutes."
},
{
"brand": "Toaks",
"model": "Titanium 750ml Pot",
"category": "cooking",
"weightGrams": 103,
"priceCents": 3300,
"description": "Ultralight titanium pot with lid and foldable handles, 750ml capacity."
},
{
"brand": "Katadyn",
"model": "BeFree 1.0L",
"category": "hydration",
"weightGrams": 59,
"priceCents": 4500,
"description": "Ultralight hollow fiber water filter with 0.1 micron filtration and 1L soft flask."
},
{
"brand": "HydraPak",
"model": "Seeker 2L",
"category": "hydration",
"weightGrams": 73,
"priceCents": 1800,
"description": "Collapsible 2L water storage with wide mouth and compatible with Katadyn BeFree filter."
},
{
"brand": "Nitecore",
"model": "NU25 UL",
"category": "lighting",
"weightGrams": 28,
"priceCents": 3600,
"description": "Ultralight USB-C rechargeable headlamp with 400 lumens max and red light mode."
},
{
"brand": "Exposure Lights",
"model": "Revo Dynamo",
"category": "lighting",
"weightGrams": 130,
"priceCents": 22000,
"description": "Dynamo-powered front light with 800 lumens, built-in standlight, and USB charging output."
},
{
"brand": "Surly",
"model": "24-Pack Rack",
"category": "racks",
"weightGrams": 750,
"priceCents": 10000,
"description": "Front rack for bikepacking with 24-pack platform, fits most forks with mid-blade eyelets."
},
{
"brand": "Salsa",
"model": "Anything Cage HD",
"category": "accessories",
"weightGrams": 80,
"priceCents": 2500,
"description": "Heavy-duty bottle cage for oversized loads like dry bags and fuel canisters."
}
]

View File

@@ -127,6 +127,31 @@ export const apiKeys = sqliteTable("api_keys", {
.$defaultFn(() => new Date()), .$defaultFn(() => new Date()),
}); });
export const globalItems = sqliteTable("global_items", {
id: integer("id").primaryKey({ autoIncrement: true }),
brand: text("brand").notNull(),
model: text("model").notNull(),
category: text("category"),
weightGrams: real("weight_grams"),
priceCents: integer("price_cents"),
imageUrl: text("image_url"),
description: text("description"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
export const itemGlobalLinks = sqliteTable("item_global_links", {
id: integer("id").primaryKey({ autoIncrement: true }),
itemId: integer("item_id")
.notNull()
.references(() => items.id, { onDelete: "cascade" })
.unique(),
globalItemId: integer("global_item_id")
.notNull()
.references(() => globalItems.id, { onDelete: "cascade" }),
});
export const oauthClients = sqliteTable("oauth_clients", { export const oauthClients = sqliteTable("oauth_clients", {
id: integer("id").primaryKey({ autoIncrement: true }), id: integer("id").primaryKey({ autoIncrement: true }),
clientId: text("client_id").notNull().unique(), clientId: text("client_id").notNull().unique(),

View File

@@ -89,3 +89,12 @@ export const classificationSchema = z.enum(["base", "worn", "consumable"]);
export const updateClassificationSchema = z.object({ export const updateClassificationSchema = z.object({
classification: classificationSchema, classification: classificationSchema,
}); });
// Global item schemas
export const searchGlobalItemsSchema = z.object({
q: z.string().optional(),
});
export const linkItemSchema = z.object({
globalItemId: z.number().int().positive(),
});

View File

@@ -1,6 +1,8 @@
import type { z } from "zod"; import type { z } from "zod";
import type { import type {
categories, categories,
globalItems,
itemGlobalLinks,
items, items,
setupItems, setupItems,
setups, setups,
@@ -13,8 +15,10 @@ import type {
createItemSchema, createItemSchema,
createSetupSchema, createSetupSchema,
createThreadSchema, createThreadSchema,
linkItemSchema,
reorderCandidatesSchema, reorderCandidatesSchema,
resolveThreadSchema, resolveThreadSchema,
searchGlobalItemsSchema,
syncSetupItemsSchema, syncSetupItemsSchema,
updateCandidateSchema, updateCandidateSchema,
updateCategorySchema, updateCategorySchema,
@@ -49,3 +53,7 @@ export type Thread = typeof threads.$inferSelect;
export type ThreadCandidate = typeof threadCandidates.$inferSelect; export type ThreadCandidate = typeof threadCandidates.$inferSelect;
export type Setup = typeof setups.$inferSelect; export type Setup = typeof setups.$inferSelect;
export type SetupItem = typeof setupItems.$inferSelect; export type SetupItem = typeof setupItems.$inferSelect;
export type GlobalItem = typeof globalItems.$inferSelect;
export type ItemGlobalLink = typeof itemGlobalLinks.$inferSelect;
export type SearchGlobalItems = z.infer<typeof searchGlobalItemsSchema>;
export type LinkItem = z.infer<typeof linkItemSchema>;

View File

@@ -0,0 +1,172 @@
import { beforeEach, describe, expect, it } from "bun:test";
import { globalItems, itemGlobalLinks, items } from "../../src/db/schema.ts";
import {
getGlobalItemWithOwnerCount,
linkItemToGlobal,
searchGlobalItems,
unlinkItemFromGlobal,
} from "../../src/server/services/global-item.service.ts";
import { seedGlobalItems } from "../../src/db/seed-global-items.ts";
import { createTestDb } from "../helpers/db.ts";
type TestDb = ReturnType<typeof createTestDb>;
function insertGlobalItem(
db: TestDb,
data: {
brand: string;
model: string;
category?: string;
weightGrams?: number;
priceCents?: number;
},
) {
return db
.insert(globalItems)
.values({
brand: data.brand,
model: data.model,
category: data.category ?? null,
weightGrams: data.weightGrams ?? null,
priceCents: data.priceCents ?? null,
})
.returning()
.get();
}
function insertItem(db: TestDb, name: string) {
return db
.insert(items)
.values({ name, categoryId: 1 })
.returning()
.get();
}
describe("Global Item Service", () => {
let db: TestDb;
beforeEach(() => {
db = createTestDb();
});
describe("searchGlobalItems", () => {
it("returns all global items when no query provided", () => {
insertGlobalItem(db, { brand: "Revelate Designs", model: "Terrapin System" });
insertGlobalItem(db, { brand: "Apidura", model: "Handlebar Pack" });
const results = searchGlobalItems(db);
expect(results).toHaveLength(2);
});
it("returns items matching brand (case-insensitive)", () => {
insertGlobalItem(db, { brand: "Revelate Designs", model: "Terrapin System" });
insertGlobalItem(db, { brand: "Apidura", model: "Handlebar Pack" });
const results = searchGlobalItems(db, "revelate");
expect(results).toHaveLength(1);
expect(results[0].brand).toBe("Revelate Designs");
});
it("returns items matching model (case-insensitive)", () => {
insertGlobalItem(db, { brand: "Revelate Designs", model: "Terrapin System" });
insertGlobalItem(db, { brand: "Apidura", model: "Handlebar Pack" });
const results = searchGlobalItems(db, "HANDLEBAR");
expect(results).toHaveLength(1);
expect(results[0].model).toBe("Handlebar Pack");
});
it("does not match everything with wildcard chars", () => {
insertGlobalItem(db, { brand: "Revelate Designs", model: "Terrapin System" });
insertGlobalItem(db, { brand: "Apidura", model: "Handlebar Pack" });
const results = searchGlobalItems(db, "100%");
expect(results).toHaveLength(0);
});
});
describe("getGlobalItemWithOwnerCount", () => {
it("returns item with ownerCount 0 when no links", () => {
const gi = insertGlobalItem(db, { brand: "MSR", model: "PocketRocket 2" });
const result = getGlobalItemWithOwnerCount(db, gi.id);
expect(result).not.toBeNull();
expect(result!.ownerCount).toBe(0);
expect(result!.brand).toBe("MSR");
});
it("returns ownerCount matching number of linked items", () => {
const gi = insertGlobalItem(db, { brand: "MSR", model: "PocketRocket 2" });
const item1 = insertItem(db, "My Stove");
const item2 = insertItem(db, "Another Stove");
db.insert(itemGlobalLinks)
.values({ itemId: item1.id, globalItemId: gi.id })
.run();
db.insert(itemGlobalLinks)
.values({ itemId: item2.id, globalItemId: gi.id })
.run();
const result = getGlobalItemWithOwnerCount(db, gi.id);
expect(result).not.toBeNull();
expect(result!.ownerCount).toBe(2);
});
it("returns null for non-existent id", () => {
const result = getGlobalItemWithOwnerCount(db, 9999);
expect(result).toBeNull();
});
});
describe("linkItemToGlobal", () => {
it("creates link and returns link row", () => {
const gi = insertGlobalItem(db, { brand: "MSR", model: "PocketRocket 2" });
const item = insertItem(db, "My Stove");
const link = linkItemToGlobal(db, item.id, gi.id);
expect(link.itemId).toBe(item.id);
expect(link.globalItemId).toBe(gi.id);
});
it("throws when item already linked", () => {
const gi = insertGlobalItem(db, { brand: "MSR", model: "PocketRocket 2" });
const item = insertItem(db, "My Stove");
linkItemToGlobal(db, item.id, gi.id);
expect(() => linkItemToGlobal(db, item.id, gi.id)).toThrow();
});
});
describe("unlinkItemFromGlobal", () => {
it("removes the link", () => {
const gi = insertGlobalItem(db, { brand: "MSR", model: "PocketRocket 2" });
const item = insertItem(db, "My Stove");
linkItemToGlobal(db, item.id, gi.id);
const deleted = unlinkItemFromGlobal(db, item.id);
expect(deleted).toBe(1);
// Verify link is gone
const result = getGlobalItemWithOwnerCount(db, gi.id);
expect(result!.ownerCount).toBe(0);
});
});
describe("seedGlobalItems", () => {
it("inserts seed data on first call", () => {
seedGlobalItems(db);
const all = db.select().from(globalItems).all();
expect(all.length).toBeGreaterThan(0);
});
it("is idempotent on second call", () => {
seedGlobalItems(db);
const countAfterFirst = db.select().from(globalItems).all().length;
seedGlobalItems(db);
const countAfterSecond = db.select().from(globalItems).all().length;
expect(countAfterSecond).toBe(countAfterFirst);
});
});
});