|
1 | | -import { describe, it, expect, vi, beforeEach } from 'vitest' |
2 | | -import { renderHook, waitFor } from '@testing-library/react' |
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { renderHook } from '@testing-library/react' |
3 | 3 | import { usePatternFiles } from './use-pattern-files' |
| 4 | +import type { CookbookItem } from './use-cookbook' |
4 | 5 |
|
5 | | -describe('usePatternFiles', () => { |
6 | | - beforeEach(() => { |
7 | | - vi.restoreAllMocks() |
8 | | - }) |
| 6 | +function makeItem(overrides: Partial<CookbookItem> = {}): CookbookItem { |
| 7 | + return { |
| 8 | + name: 'test-pattern', |
| 9 | + type: 'registry:pattern', |
| 10 | + title: 'Test Pattern', |
| 11 | + files: [ |
| 12 | + { path: 'patterns/test/saga.star', content: 'def execute():\n pass' }, |
| 13 | + { path: 'patterns/test/manifest.yaml', content: 'name: test\ntype: registry:pattern' }, |
| 14 | + ], |
| 15 | + ...overrides, |
| 16 | + } |
| 17 | +} |
9 | 18 |
|
10 | | - it('returns null content when no pattern name provided', () => { |
| 19 | +describe('usePatternFiles', () => { |
| 20 | + it('returns empty state when no item provided', () => { |
11 | 21 | const { result } = renderHook(() => usePatternFiles(undefined)) |
12 | | - expect(result.current.starlarkContent).toBeNull() |
| 22 | + expect(result.current.starlarkFiles).toEqual([]) |
13 | 23 | expect(result.current.manifestContent).toBeNull() |
14 | 24 | expect(result.current.isLoading).toBe(false) |
15 | 25 | }) |
16 | 26 |
|
17 | | - it('fetches starlark and manifest files', async () => { |
18 | | - vi.spyOn(globalThis, 'fetch').mockImplementation(async (url) => { |
19 | | - const path = typeof url === 'string' ? url : url.toString() |
20 | | - if (path.includes('saga.star')) { |
21 | | - return new Response('def execute():\n pass', { status: 200 }) |
22 | | - } |
23 | | - if (path.includes('manifest.yaml')) { |
24 | | - return new Response('name: test\ntype: registry:pattern', { status: 200 }) |
25 | | - } |
26 | | - return new Response('', { status: 404 }) |
27 | | - }) |
28 | | - |
29 | | - const { result } = renderHook(() => usePatternFiles('test-pattern')) |
| 27 | + it('returns empty state for UI component items', () => { |
| 28 | + const item = makeItem({ type: 'registry:ui' }) |
| 29 | + const { result } = renderHook(() => usePatternFiles(item)) |
| 30 | + expect(result.current.starlarkFiles).toEqual([]) |
| 31 | + expect(result.current.manifestContent).toBeNull() |
| 32 | + }) |
30 | 33 |
|
31 | | - await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 34 | + it('extracts starlark and manifest content from bundled files', () => { |
| 35 | + const item = makeItem() |
| 36 | + const { result } = renderHook(() => usePatternFiles(item)) |
32 | 37 |
|
33 | | - expect(result.current.starlarkContent).toBe('def execute():\n pass') |
| 38 | + expect(result.current.starlarkFiles).toEqual([ |
| 39 | + { name: 'saga.star', content: 'def execute():\n pass' }, |
| 40 | + ]) |
34 | 41 | expect(result.current.manifestContent).toBe('name: test\ntype: registry:pattern') |
| 42 | + expect(result.current.isLoading).toBe(false) |
35 | 43 | }) |
36 | 44 |
|
37 | | - it('returns null for files that return 404', async () => { |
38 | | - vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response('', { status: 404 })) |
39 | | - |
40 | | - const { result } = renderHook(() => usePatternFiles('missing-pattern')) |
| 45 | + it('handles multiple starlark files', () => { |
| 46 | + const item = makeItem({ |
| 47 | + files: [ |
| 48 | + { path: 'patterns/test/billing.star', content: 'def billing(): pass' }, |
| 49 | + { path: 'patterns/test/onboarding.star', content: 'def onboard(): pass' }, |
| 50 | + { path: 'patterns/test/manifest.yaml', content: 'name: test' }, |
| 51 | + ], |
| 52 | + }) |
| 53 | + const { result } = renderHook(() => usePatternFiles(item)) |
41 | 54 |
|
42 | | - await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 55 | + expect(result.current.starlarkFiles).toHaveLength(2) |
| 56 | + expect(result.current.starlarkFiles[0].name).toBe('billing.star') |
| 57 | + expect(result.current.starlarkFiles[1].name).toBe('onboarding.star') |
| 58 | + }) |
43 | 59 |
|
44 | | - expect(result.current.starlarkContent).toBeNull() |
| 60 | + it('returns null manifest when no yaml file present', () => { |
| 61 | + const item = makeItem({ |
| 62 | + files: [{ path: 'patterns/test/saga.star', content: 'def execute(): pass' }], |
| 63 | + }) |
| 64 | + const { result } = renderHook(() => usePatternFiles(item)) |
45 | 65 | expect(result.current.manifestContent).toBeNull() |
46 | 66 | }) |
47 | 67 |
|
48 | | - it('returns null for fetch errors', async () => { |
49 | | - vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('Network error')) |
50 | | - |
51 | | - const { result } = renderHook(() => usePatternFiles('broken-pattern')) |
52 | | - |
53 | | - await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 68 | + it('rejects HTML content as invalid (SPA fallback protection)', () => { |
| 69 | + const item = makeItem({ |
| 70 | + files: [ |
| 71 | + { path: 'patterns/test/saga.star', content: '<!DOCTYPE html><html></html>' }, |
| 72 | + { path: 'patterns/test/manifest.yaml', content: '<html><body>not yaml</body></html>' }, |
| 73 | + ], |
| 74 | + }) |
| 75 | + const { result } = renderHook(() => usePatternFiles(item)) |
| 76 | + expect(result.current.starlarkFiles).toEqual([]) |
| 77 | + expect(result.current.manifestContent).toBeNull() |
| 78 | + }) |
54 | 79 |
|
55 | | - expect(result.current.starlarkContent).toBeNull() |
| 80 | + it('handles files with no content', () => { |
| 81 | + const item = makeItem({ |
| 82 | + files: [ |
| 83 | + { path: 'patterns/test/saga.star' }, |
| 84 | + { path: 'patterns/test/manifest.yaml' }, |
| 85 | + ], |
| 86 | + }) |
| 87 | + const { result } = renderHook(() => usePatternFiles(item)) |
| 88 | + expect(result.current.starlarkFiles).toEqual([]) |
56 | 89 | expect(result.current.manifestContent).toBeNull() |
57 | 90 | }) |
58 | 91 | }) |
0 commit comments