Files
supabase/apps/studio/lib/eszip-parser.test.ts
T
냥냐챠 d29db69573 fix(edge-functions): parse eszip made from edge-runtime v1.69.x correctly (#38296)
* fix(edge-functions): parse eszip made from edge-runtime v1.69.x correctly

* stamp: update test

* stamp: format

* stamp: meow

* stamp: meowmeow

* stamp: format
2025-09-02 10:23:22 +09:00

123 lines
3.3 KiB
TypeScript

import { Parser } from '@deno/eszip'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { parseEszip } from './eszip-parser'
vi.mock('@deno/eszip', () => ({
Parser: {
createInstance: vi.fn(),
},
}))
vi.stubGlobal(
'File',
class MockFile {
name: string
content: string
constructor(content: string[], name: string) {
this.name = name
this.content = content[0]
}
async text() {
return this.content
}
}
)
vi.stubGlobal(
'URL',
class MockURL {
pathname: string
constructor(url: string) {
this.pathname = url
}
}
)
describe('eszip-parser', () => {
const mockParser = {
parseBytes: vi.fn(),
load: vi.fn(),
getModuleSource: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
;(Parser.createInstance as any).mockResolvedValue(mockParser)
})
afterEach(() => {
vi.resetAllMocks()
})
describe('parseEszip', () => {
it('should successfully parse and extract files from eszip', async () => {
const mockBytes = new Uint8Array([1, 2, 3])
const mockSpecifiers = ['file1.ts', 'file2.ts']
const mockModuleSource1 = 'export const hello = "world"'
const mockModuleSource2 = 'export const foo = "bar"'
mockParser.parseBytes.mockResolvedValue(mockSpecifiers)
mockParser.load.mockResolvedValue(undefined)
mockParser.getModuleSource
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(mockModuleSource1)
.mockResolvedValueOnce(mockModuleSource2)
const result = await parseEszip(mockBytes)
expect(Parser.createInstance).toHaveBeenCalledTimes(1)
expect(mockParser.parseBytes).toHaveBeenCalledWith(mockBytes)
expect(mockParser.load).toHaveBeenCalled()
expect(result.version).toEqual(0)
expect(result.files).toHaveLength(2)
expect(result.files[0]).toEqual({
name: 'file1.ts',
content: mockModuleSource1,
})
expect(result.files[1]).toEqual({
name: 'file2.ts',
content: mockModuleSource2,
})
})
it('should handle parseBytes failure', async () => {
mockParser.parseBytes.mockRejectedValue(new Error('Parse error'))
await expect(parseEszip(new Uint8Array())).rejects.toThrow('Parse error')
})
it('should handle load failure', async () => {
mockParser.parseBytes.mockResolvedValue(['file1.ts'])
mockParser.load.mockRejectedValue(new Error('Load error'))
await expect(parseEszip(new Uint8Array())).rejects.toThrow('Load error')
})
it('should filter out unwanted specifiers', async () => {
const mockBytes = new Uint8Array([1, 2, 3])
const mockSpecifiers = [
'file1.ts',
'npm:package',
'https://example.com/file.ts',
'file2.ts',
'---internal',
'jsr:package',
]
const mockModuleSource = 'export const test = "test"'
mockParser.parseBytes.mockResolvedValue(mockSpecifiers)
mockParser.load.mockResolvedValue(undefined)
mockParser.getModuleSource.mockResolvedValue(mockModuleSource)
const result = await parseEszip(mockBytes)
// Only file1.ts and file2.ts should be included
expect(result.version).toEqual(0)
expect(result.files).toHaveLength(2)
expect(result.files[0].name).toBe('file1.ts')
expect(result.files[1].name).toBe('file2.ts')
})
})
})