|
| 1 | +import { SandboxSettings, SandboxSettingsConfig } from '../sandbox' |
| 2 | + |
| 3 | +describe(SandboxSettings, () => { |
| 4 | + const edgeFnResponseBody = `function processSignal() { console.log('hello world') }` |
| 5 | + const baseSettings: SandboxSettingsConfig = { |
| 6 | + functionHost: undefined, |
| 7 | + processSignal: undefined, |
| 8 | + edgeFnDownloadURL: 'http://example.com/download', |
| 9 | + edgeFnFetchClient: jest.fn().mockReturnValue( |
| 10 | + Promise.resolve({ |
| 11 | + text: () => edgeFnResponseBody, |
| 12 | + }) |
| 13 | + ), |
| 14 | + } |
| 15 | + test('initializes with provided settings', async () => { |
| 16 | + const sandboxSettings = new SandboxSettings({ ...baseSettings }) |
| 17 | + expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith( |
| 18 | + baseSettings.edgeFnDownloadURL |
| 19 | + ) |
| 20 | + expect(await sandboxSettings.processSignal).toEqual(edgeFnResponseBody) |
| 21 | + }) |
| 22 | + |
| 23 | + test('normalizes edgeFnDownloadURL when functionHost is provided', async () => { |
| 24 | + const settings: SandboxSettingsConfig = { |
| 25 | + ...baseSettings, |
| 26 | + processSignal: undefined, |
| 27 | + functionHost: 'newHost.com', |
| 28 | + edgeFnDownloadURL: 'https://original.com/download', |
| 29 | + } |
| 30 | + new SandboxSettings(settings) |
| 31 | + expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith( |
| 32 | + 'https://newHost.com/download' |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + test('creates default processSignal when parameters are missing', async () => { |
| 37 | + const consoleWarnSpy = jest |
| 38 | + .spyOn(console, 'warn') |
| 39 | + .mockImplementation(() => {}) |
| 40 | + const settings: SandboxSettingsConfig = { |
| 41 | + ...baseSettings, |
| 42 | + processSignal: undefined, |
| 43 | + edgeFnDownloadURL: undefined, |
| 44 | + } |
| 45 | + const sandboxSettings = new SandboxSettings(settings) |
| 46 | + expect(await sandboxSettings.processSignal).toEqual( |
| 47 | + 'globalThis.processSignal = function processSignal() {}' |
| 48 | + ) |
| 49 | + expect(baseSettings.edgeFnFetchClient).not.toHaveBeenCalled() |
| 50 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 51 | + expect.stringContaining('processSignal') |
| 52 | + ) |
| 53 | + }) |
| 54 | +}) |
0 commit comments