|
1 | | -import { describe, expect, it } from 'vitest' |
| 1 | +import { describe, expect, it, vi } from 'vitest' |
2 | 2 | import type { JsDelivrFileNode, PackageFileTree } from '../../shared/types' |
3 | | -import { convertToFileTree } from '../../server/utils/file-tree' |
| 3 | +import { convertToFileTree, fetchFileTree, getPackageFileTree } from '../../server/utils/file-tree' |
4 | 4 |
|
5 | 5 | const getChildren = (node?: PackageFileTree): PackageFileTree[] => node?.children ?? [] |
6 | 6 |
|
@@ -73,3 +73,101 @@ describe('convertToFileTree', () => { |
73 | 73 | expect(tree).toEqual(empty) |
74 | 74 | }) |
75 | 75 | }) |
| 76 | + |
| 77 | +describe('fetchFileTree', () => { |
| 78 | + it('returns parsed json when response is ok', async () => { |
| 79 | + const body = { |
| 80 | + type: 'npm', |
| 81 | + name: 'pkg', |
| 82 | + version: '1.0.0', |
| 83 | + default: 'index.js', |
| 84 | + files: [], |
| 85 | + } |
| 86 | + |
| 87 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 88 | + ok: true, |
| 89 | + json: async () => body, |
| 90 | + }) |
| 91 | + |
| 92 | + vi.stubGlobal('fetch', fetchMock) |
| 93 | + |
| 94 | + try { |
| 95 | + const result = await fetchFileTree('pkg', '1.0.0') |
| 96 | + expect(result).toEqual(body) |
| 97 | + } finally { |
| 98 | + vi.unstubAllGlobals() |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + it('throws a 404 error when package is not found', async () => { |
| 103 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 104 | + ok: false, |
| 105 | + status: 404, |
| 106 | + }) |
| 107 | + |
| 108 | + const createErrorMock = vi.fn((opts: { statusCode: number; message: string }) => opts) |
| 109 | + |
| 110 | + vi.stubGlobal('fetch', fetchMock) |
| 111 | + vi.stubGlobal('createError', createErrorMock) |
| 112 | + |
| 113 | + try { |
| 114 | + await expect(fetchFileTree('pkg', '1.0.0')).rejects.toMatchObject({ statusCode: 404 }) |
| 115 | + } finally { |
| 116 | + vi.unstubAllGlobals() |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + it('throws a 502 error for non-404 failures', async () => { |
| 121 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 122 | + ok: false, |
| 123 | + status: 500, |
| 124 | + }) |
| 125 | + |
| 126 | + const createErrorMock = vi.fn((opts: { statusCode: number; message: string }) => opts) |
| 127 | + |
| 128 | + vi.stubGlobal('fetch', fetchMock) |
| 129 | + vi.stubGlobal('createError', createErrorMock) |
| 130 | + |
| 131 | + try { |
| 132 | + await expect(fetchFileTree('pkg', '1.0.0')).rejects.toMatchObject({ statusCode: 502 }) |
| 133 | + } finally { |
| 134 | + vi.unstubAllGlobals() |
| 135 | + } |
| 136 | + }) |
| 137 | +}) |
| 138 | + |
| 139 | +describe('getPackageFileTree', () => { |
| 140 | + it('returns metadata and a converted tree', async () => { |
| 141 | + const body = { |
| 142 | + type: 'npm', |
| 143 | + name: 'pkg', |
| 144 | + version: '1.0.0', |
| 145 | + default: 'index.js', |
| 146 | + files: [ |
| 147 | + { |
| 148 | + type: 'directory', |
| 149 | + name: 'src', |
| 150 | + files: [{ type: 'file', name: 'index.js', size: 5 }], |
| 151 | + }, |
| 152 | + ], |
| 153 | + } |
| 154 | + |
| 155 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 156 | + ok: true, |
| 157 | + json: async () => body, |
| 158 | + }) |
| 159 | + |
| 160 | + vi.stubGlobal('fetch', fetchMock) |
| 161 | + |
| 162 | + try { |
| 163 | + const result = await getPackageFileTree('pkg', '1.0.0') |
| 164 | + expect(result.package).toBe('pkg') |
| 165 | + expect(result.version).toBe('1.0.0') |
| 166 | + expect(result.default).toBe('index.js') |
| 167 | + expect(result.tree[0]?.path).toBe('src') |
| 168 | + expect(result.tree[0]?.children?.[0]?.path).toBe('src/index.js') |
| 169 | + } finally { |
| 170 | + vi.unstubAllGlobals() |
| 171 | + } |
| 172 | + }) |
| 173 | +}) |
0 commit comments