Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import {
3 MarblePlanError,
4 downloadMarbleAsset,
5 isAllowedMarbleSplatUrl,
6 safeDownloadName,
7} from './marble-splat'
8
9describe('isAllowedMarbleSplatUrl', () => {
10 it.each([
11 'https://marble.worldlabs.ai/world/1.spz',
12 'https://cdn.worldlabs.ai/a.spz',
13 'https://storage.googleapis.com/bucket/a.spz',
14 'https://abc.googleusercontent.com/a.spz',
15 ])('allows %s', (url) => {
16 expect(isAllowedMarbleSplatUrl(url)).toBe(true)
17 })
18
19 it.each([
20 'http://marble.worldlabs.ai/a.spz',
21 'https://evil.com/a.spz',
22 'https://user:pass@worldlabs.ai/a.spz',
23 'https://worldlabs.ai.evil.com/a.spz',
24 'not-a-url',
25 '',
26 ])('rejects %s', (url) => {
27 expect(isAllowedMarbleSplatUrl(url)).toBe(false)
28 })
29})
30
31describe('safeDownloadName', () => {
32 it('keeps a simple filename', () => {
33 expect(safeDownloadName('world.spz')).toBe('world.spz')
34 })
35
36 it('rejects a path', () => {
37 expect(safeDownloadName('../x.spz')).toBeUndefined()
38 })
39})
40
41describe('downloadMarbleAsset', () => {
42 afterEach(() => {
43 vi.unstubAllGlobals()
44 })
45
46 it.each([402, 403])('throws MarblePlanError on %s', async (status) => {
47 vi.stubGlobal(
48 'fetch',
49 vi.fn(async () => new Response('denied', { status })),
50 )
51 await expect(
52 downloadMarbleAsset('https://marble.worldlabs.ai/a.spz', 'world.spz'),
53 ).rejects.toBeInstanceOf(MarblePlanError)
54 })
55
56 it('throws on other non-OK statuses', async () => {
57 vi.stubGlobal(
58 'fetch',
59 vi.fn(async () => new Response('nope', { status: 500 })),
60 )
61 await expect(
62 downloadMarbleAsset('https://marble.worldlabs.ai/a.spz', 'world.spz'),
63 ).rejects.toThrow(/Download failed/)
64 })
65
66 it('throws when the file is empty', async () => {
67 vi.stubGlobal(
68 'fetch',
69 vi.fn(async () => new Response(new Blob(), { status: 200 })),
70 )
71 await expect(
72 downloadMarbleAsset('https://marble.worldlabs.ai/a.spz', 'world.spz'),
73 ).rejects.toThrow(/Download failed/)
74 })
75})