-
Notifications
You must be signed in to change notification settings - Fork 0
feat: multi-product registry — ProductRegistry schema, parser, and /api/products routes #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fdd504c
feat(@helm/shared): add ProductRegistry schema and parser
lhpaul 833b643
feat(@helm/api): add getProductRegistry singleton and /api/products r…
lhpaul 85d9b33
fix(@helm/api): validate slug params with Zod and return defensive co…
lhpaul 46e69d8
fix(@helm/api): narrow ENOENT fallback to registry file itself in get…
lhpaul 665c0f2
fix(@helm/api): deep-clone Product objects in getProductRegistry with…
lhpaul f55fd78
fix(@helm/api): deep-clone per-caller when returning in-flight regist…
lhpaul 8cc9fc6
fix(@helm/api): validate HELM_KNOWLEDGE_REPO_PATH against filesystem …
lhpaul bba633d
fix(@helm/api): fix ENOENT fallback in getProductRegistry — check cau…
lhpaul a0310bc
fix(@helm/api): baseDir for products.yaml path resolution is knowledg…
lhpaul 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
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,171 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { app } from '../app.js'; | ||
| import { _resetForTests } from '../services/index.js'; | ||
|
|
||
| // ── Fixtures ────────────────────────────────────────────────────────────────── | ||
|
|
||
| const HELM_PRODUCT = { | ||
| helm_version: '0', | ||
| product: { slug: 'helm', name: 'Helm' }, | ||
| workflow: { stages_enabled: ['discovery', 'released'], designer_gate: 'skip', qa_gate: 'skip' }, | ||
| }; | ||
|
|
||
| const PLAYGROUND_PRODUCT = { | ||
| helm_version: '0', | ||
| product: { slug: 'helm-playground', name: 'Helm Playground' }, | ||
| workflow: { stages_enabled: ['discovery', 'released'], designer_gate: 'skip', qa_gate: 'skip' }, | ||
| }; | ||
|
|
||
| const HELM_ITEMS = [ | ||
| { | ||
| externalId: 'issue_1', | ||
| productSlug: 'helm', | ||
| currentStage: 'discovery', | ||
| history: [], | ||
| createdAt: '2026-01-01T00:00:00Z', | ||
| updatedAt: '2026-01-01T00:00:00Z', | ||
| }, | ||
| { | ||
| externalId: 'issue_2', | ||
| productSlug: 'helm', | ||
| currentStage: 'released', | ||
| history: [], | ||
| createdAt: '2026-01-02T00:00:00Z', | ||
| updatedAt: '2026-01-02T00:00:00Z', | ||
| }, | ||
| ]; | ||
|
|
||
| const PLAYGROUND_ITEMS = [ | ||
| { | ||
| externalId: 'issue_10', | ||
| productSlug: 'helm-playground', | ||
| currentStage: 'discovery', | ||
| history: [], | ||
| createdAt: '2026-01-03T00:00:00Z', | ||
| updatedAt: '2026-01-03T00:00:00Z', | ||
| }, | ||
| ]; | ||
|
|
||
| // ── Mocks ───────────────────────────────────────────────────────────────────── | ||
|
|
||
| const { mockGetProductRegistry, mockList } = vi.hoisted(() => ({ | ||
| mockGetProductRegistry: vi.fn(), | ||
| mockList: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../services/index.js', async (importOriginal) => { | ||
| const real = await importOriginal<typeof import('../services/index.js')>(); | ||
| return { | ||
| ...real, | ||
| getProductRegistry: mockGetProductRegistry, | ||
| getItemStore: vi.fn().mockResolvedValue({ list: mockList }), | ||
| }; | ||
| }); | ||
|
|
||
| // ── Tests ───────────────────────────────────────────────────────────────────── | ||
|
|
||
| describe('GET /api/products', () => { | ||
| beforeEach(() => { | ||
| _resetForTests(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('GET /api/products', () => { | ||
| it('returns all registered products', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT, PLAYGROUND_PRODUCT]); | ||
| const res = await app.request('/api/products'); | ||
| expect(res.status).toBe(200); | ||
| const body = (await res.json()) as (typeof HELM_PRODUCT)[]; | ||
| expect(body).toHaveLength(2); | ||
| expect(body.map((p) => p.product.slug).sort()).toEqual(['helm', 'helm-playground']); | ||
| }); | ||
|
|
||
| it('returns 500 when registry loading fails', async () => { | ||
| mockGetProductRegistry.mockRejectedValue(new Error('disk error')); | ||
| const res = await app.request('/api/products'); | ||
| expect(res.status).toBe(500); | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET /api/products/:slug', () => { | ||
| it('returns 400 for invalid slug format', async () => { | ||
| const res = await app.request('/api/products/INVALID_SLUG!'); | ||
| expect(res.status).toBe(400); | ||
| }); | ||
|
|
||
| it('returns the matching product', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT, PLAYGROUND_PRODUCT]); | ||
| const res = await app.request('/api/products/helm-playground'); | ||
| expect(res.status).toBe(200); | ||
| const body = (await res.json()) as typeof PLAYGROUND_PRODUCT; | ||
| expect(body.product.slug).toBe('helm-playground'); | ||
| }); | ||
|
|
||
| it('returns 404 for unknown slug', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT]); | ||
| const res = await app.request('/api/products/nonexistent'); | ||
| expect(res.status).toBe(404); | ||
| }); | ||
|
|
||
| it('returns 500 when registry loading fails', async () => { | ||
| mockGetProductRegistry.mockRejectedValue(new Error('disk error')); | ||
| const res = await app.request('/api/products/helm'); | ||
| expect(res.status).toBe(500); | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET /api/products/:slug/items', () => { | ||
| it('returns only items belonging to the requested product', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT, PLAYGROUND_PRODUCT]); | ||
| mockList.mockResolvedValue([...HELM_ITEMS, ...PLAYGROUND_ITEMS]); | ||
|
|
||
| const res = await app.request('/api/products/helm/items'); | ||
| expect(res.status).toBe(200); | ||
| const body = (await res.json()) as typeof HELM_ITEMS; | ||
| expect(body).toHaveLength(2); | ||
| expect(body.every((i) => i.productSlug === 'helm')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns only playground items when requesting helm-playground', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT, PLAYGROUND_PRODUCT]); | ||
| mockList.mockResolvedValue([...HELM_ITEMS, ...PLAYGROUND_ITEMS]); | ||
|
|
||
| const res = await app.request('/api/products/helm-playground/items'); | ||
| expect(res.status).toBe(200); | ||
| const body = (await res.json()) as typeof PLAYGROUND_ITEMS; | ||
| expect(body).toHaveLength(1); | ||
| expect(body[0]!.externalId).toBe('issue_10'); | ||
| }); | ||
|
|
||
| it('returns empty array when the product has no items', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT, PLAYGROUND_PRODUCT]); | ||
| mockList.mockResolvedValue(HELM_ITEMS); | ||
|
|
||
| const res = await app.request('/api/products/helm-playground/items'); | ||
| expect(res.status).toBe(200); | ||
| expect(await res.json()).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns 400 for invalid slug format', async () => { | ||
| const res = await app.request('/api/products/BAD_SLUG!/items'); | ||
| expect(res.status).toBe(400); | ||
| }); | ||
|
|
||
| it('returns 404 when the product slug does not exist', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT]); | ||
| const res = await app.request('/api/products/ghost/items'); | ||
| expect(res.status).toBe(404); | ||
| }); | ||
|
|
||
| it('returns 500 when item store fails', async () => { | ||
| mockGetProductRegistry.mockResolvedValue([HELM_PRODUCT]); | ||
| mockList.mockRejectedValue(new Error('disk error')); | ||
| const res = await app.request('/api/products/helm/items'); | ||
| expect(res.status).toBe(500); | ||
| }); | ||
| }); | ||
| }); |
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,70 @@ | ||
| import { Hono } from 'hono'; | ||
| import { z } from 'zod'; | ||
| import { getProductRegistry, getItemStore } from '../services/index.js'; | ||
|
|
||
| export const productsRouter = new Hono(); | ||
|
|
||
| // Slug format mirrors product.schema.ts: lowercase alphanumeric + hyphens. | ||
| const SlugParamsSchema = z | ||
| .object({ | ||
| slug: z | ||
| .string() | ||
| .min(1) | ||
| .regex(/^[a-z0-9-]+$/, 'Invalid product slug format'), | ||
| }) | ||
| .strict(); | ||
|
|
||
| function parseSlug(raw: string): string | null { | ||
| const result = SlugParamsSchema.safeParse({ slug: raw }); | ||
| return result.success ? result.data.slug : null; | ||
| } | ||
|
|
||
| // ── GET /api/products ───────────────────────────────────────────────────────── | ||
|
|
||
| productsRouter.get('/products', async (c) => { | ||
| try { | ||
| const products = await getProductRegistry(); | ||
| return c.json(products); | ||
| } catch (err) { | ||
| console.error('[products] Failed to load product registry:', err); | ||
| return c.json({ error: 'Failed to load product registry' }, 500); | ||
| } | ||
| }); | ||
|
|
||
| // ── GET /api/products/:slug ─────────────────────────────────────────────────── | ||
|
|
||
| productsRouter.get('/products/:slug', async (c) => { | ||
| const slug = parseSlug(c.req.param('slug')); | ||
| if (!slug) return c.json({ error: 'Invalid product slug' }, 400); | ||
|
|
||
| try { | ||
| const products = await getProductRegistry(); | ||
| const product = products.find((p) => p.product.slug === slug); | ||
| if (!product) return c.json({ error: `Product not found: ${slug}` }, 404); | ||
| return c.json(product); | ||
| } catch (err) { | ||
| console.error(`[products] Failed to load product ${slug}:`, err); | ||
| return c.json({ error: 'Failed to load product registry' }, 500); | ||
| } | ||
| }); | ||
|
|
||
| // ── GET /api/products/:slug/items ───────────────────────────────────────────── | ||
|
|
||
| productsRouter.get('/products/:slug/items', async (c) => { | ||
| const slug = parseSlug(c.req.param('slug')); | ||
| if (!slug) return c.json({ error: 'Invalid product slug' }, 400); | ||
|
|
||
| try { | ||
| const products = await getProductRegistry(); | ||
| const product = products.find((p) => p.product.slug === slug); | ||
| if (!product) return c.json({ error: `Product not found: ${slug}` }, 404); | ||
|
|
||
| const store = await getItemStore(); | ||
| const allItems = await store.list(); | ||
| const items = allItems.filter((item) => item.productSlug === slug); | ||
| return c.json(items); | ||
| } catch (err) { | ||
| console.error(`[products] Failed to load items for ${slug}:`, err); | ||
| return c.json({ error: 'Failed to load items' }, 500); | ||
| } | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.