|
| 1 | +import { dirname, join, sep } from 'node:path'; |
| 2 | +import { makeTestTempDir } from '../../test-helpers/makeFileStructure.mts'; |
| 3 | +import { anyFileFinder } from './anyFileFinder.mts'; |
| 4 | +import 'lean-test'; |
| 5 | + |
| 6 | +describe('anyFileFinder', () => { |
| 7 | + describe('given a directory', () => { |
| 8 | + const TEST_DIR = makeTestTempDir('aff-', { 'file.txt': 'Content' }); |
| 9 | + |
| 10 | + it('loads directories dynamically', async ({ getTyped }) => { |
| 11 | + const fileFinder = await anyFileFinder(getTyped(TEST_DIR), {}); |
| 12 | + expect(fileFinder.isStaticListing).isFalse(); |
| 13 | + const file = await fileFinder.find(['file.txt']); |
| 14 | + await file?.handle.close(); |
| 15 | + expect(file?.stats.size).equals(7); |
| 16 | + }); |
| 17 | + |
| 18 | + it('loads directories statically', async ({ getTyped }) => { |
| 19 | + const fileFinder = await anyFileFinder(getTyped(TEST_DIR), { mode: 'static-paths' }); |
| 20 | + expect(fileFinder.isStaticListing).isTrue(); |
| 21 | + const file = await fileFinder.find(['file.txt']); |
| 22 | + await file?.handle.close(); |
| 23 | + expect(file?.stats.size).equals(7); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('loads zip contents', async () => { |
| 28 | + const testZip = join(dirname(new URL(import.meta.url).pathname), 'test-assets.zip'); |
| 29 | + |
| 30 | + const fileFinder = await anyFileFinder(testZip, {}); |
| 31 | + expect(fileFinder.isStaticListing).isTrue(); |
| 32 | + const file1 = await fileFinder.find(['file.txt']); |
| 33 | + await file1?.handle.close(); |
| 34 | + expect(file1?.stats.size).equals(7); |
| 35 | + |
| 36 | + const file2 = await fileFinder.find(['sub', 'subfile.txt']); |
| 37 | + await file2?.handle.close(); |
| 38 | + expect(file2?.stats.size).equals(14); |
| 39 | + }); |
| 40 | + |
| 41 | + it('loads zip contents with trailing slash', async () => { |
| 42 | + const testZip = join(dirname(new URL(import.meta.url).pathname), 'test-assets.zip') + sep; |
| 43 | + |
| 44 | + const fileFinder = await anyFileFinder(testZip, {}); |
| 45 | + expect(fileFinder.isStaticListing).isTrue(); |
| 46 | + const file1 = await fileFinder.find(['file.txt']); |
| 47 | + await file1?.handle.close(); |
| 48 | + expect(file1?.stats.size).equals(7); |
| 49 | + |
| 50 | + const file2 = await fileFinder.find(['sub', 'subfile.txt']); |
| 51 | + await file2?.handle.close(); |
| 52 | + expect(file2?.stats.size).equals(14); |
| 53 | + }); |
| 54 | + |
| 55 | + it('loads nested zip contents', async () => { |
| 56 | + const testZip = join(dirname(new URL(import.meta.url).pathname), 'test-assets.zip', 'sub'); |
| 57 | + |
| 58 | + const fileFinder = await anyFileFinder(testZip, {}); |
| 59 | + expect(fileFinder.isStaticListing).isTrue(); |
| 60 | + const file = await fileFinder.find(['subfile.txt']); |
| 61 | + await file?.handle.close(); |
| 62 | + expect(file?.stats.size).equals(14); |
| 63 | + |
| 64 | + expect(await fileFinder.find(['file.txt'])).isNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('throws if given a path which does not exist', async () => { |
| 68 | + await expect(() => anyFileFinder('/does/not/exist', {})).throws( |
| 69 | + 'content to serve not found at /does/not/exist', |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments