|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | + |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { createDataLoader } from './data-loader.ts'; |
| 7 | + |
| 8 | +const baseDir = import.meta.dirname; |
| 9 | +const libertyJpg = await readFile(join(baseDir, 'test/resources/liberty.jpg')); |
| 10 | + |
| 11 | +describe('createDataLoader', () => { |
| 12 | + const loader = createDataLoader(); |
| 13 | + |
| 14 | + it('throws for invalid URLs', async () => { |
| 15 | + await expect(loader('')).rejects.toThrow("Invalid URL: ''"); |
| 16 | + await expect(loader('http://')).rejects.toThrow("Invalid URL: 'http://'"); |
| 17 | + }); |
| 18 | + |
| 19 | + it('throws for unsupported URL scheme', async () => { |
| 20 | + await expect(loader('foo:bar')).rejects.toThrow("URL not supported: 'foo:bar'"); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('http:', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.spyOn(globalThis, 'fetch').mockImplementation((req: RequestInfo | URL) => { |
| 26 | + const url = req instanceof URL ? req.href : (req as string); |
| 27 | + if (url.endsWith('image.jpg')) { |
| 28 | + return Promise.resolve(new Response(new Uint8Array([1, 2, 3]))); |
| 29 | + } |
| 30 | + return Promise.resolve(new Response('Not found', { status: 404, statusText: 'Not Found' })); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + vi.restoreAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('loads http: URL', async () => { |
| 39 | + await expect(loader('http://example.com/image.jpg')).resolves.toEqual({ |
| 40 | + data: new Uint8Array([1, 2, 3]), |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('loads https: URL', async () => { |
| 45 | + await expect(loader('https://example.com/image.jpg')).resolves.toEqual({ |
| 46 | + data: new Uint8Array([1, 2, 3]), |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('throws if 404 received', async () => { |
| 51 | + await expect(loader('https://example.com/not-there')).rejects.toThrow( |
| 52 | + 'Received 404 Not Found', |
| 53 | + ); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('data:', () => { |
| 58 | + it('loads data: URL', async () => { |
| 59 | + await expect(loader('data:image/jpeg;base64,Abc=')).resolves.toEqual({ |
| 60 | + data: new Uint8Array([1, 183]), |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('throws for invalid data: URLs', async () => { |
| 65 | + await expect(loader('data:foo')).rejects.toThrow("Invalid data URL: 'data:foo'"); |
| 66 | + }); |
| 67 | + |
| 68 | + it('throws for unsupported encoding in data: URLs', async () => { |
| 69 | + await expect(loader('data:foo,bar')).rejects.toThrow( |
| 70 | + "Unsupported encoding in data URL: 'data:foo,bar'", |
| 71 | + ); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('file:', () => { |
| 76 | + it('loads relative file: URL', async () => { |
| 77 | + const loader = createDataLoader({ resourceRoot: baseDir }); |
| 78 | + |
| 79 | + const result = await loader(`file:test/resources/liberty.jpg`); |
| 80 | + |
| 81 | + expect(result).toEqual({ data: new Uint8Array(libertyJpg) }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('loads file: URL without authority', async () => { |
| 85 | + const loader = createDataLoader({ resourceRoot: baseDir }); |
| 86 | + |
| 87 | + const result = await loader(`file:/test/resources/liberty.jpg`); |
| 88 | + |
| 89 | + expect(result).toEqual({ data: new Uint8Array(libertyJpg) }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('loads absolute file: URL with empty authority', async () => { |
| 93 | + const loader = createDataLoader({ resourceRoot: baseDir }); |
| 94 | + |
| 95 | + const result = await loader(`file:///test/resources/liberty.jpg`); |
| 96 | + |
| 97 | + expect(result).toEqual({ data: new Uint8Array(libertyJpg) }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('loads absolute file: URL with authority', async () => { |
| 101 | + const loader = createDataLoader({ resourceRoot: baseDir }); |
| 102 | + |
| 103 | + const result = await loader(`file://localhost/test/resources/liberty.jpg`); |
| 104 | + |
| 105 | + expect(result).toEqual({ data: new Uint8Array(libertyJpg) }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('rejects when no resource root directory defined', async () => { |
| 109 | + const url = `file:/test/resources/liberty.jpg`; |
| 110 | + |
| 111 | + await expect(loader(url)).rejects.toThrow('No resource root defined'); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments