Skip to content

Commit

Permalink
fix processSignal onboarding issue if no edge fn exists
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Jul 17, 2024
1 parent bec8752 commit 11ca952
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SandboxSettings, SandboxSettingsConfig } from '../sandbox'

describe(SandboxSettings, () => {
const edgeFnResponseBody = `function processSignal() { console.log('hello world') }`
const baseSettings: SandboxSettingsConfig = {
functionHost: undefined,
processSignal: undefined,
edgeFnDownloadURL: 'http://example.com/download',
edgeFnFetchClient: jest.fn().mockReturnValue(
Promise.resolve({
text: () => edgeFnResponseBody,
})
),
}
test('initializes with provided settings', async () => {
const sandboxSettings = new SandboxSettings({ ...baseSettings })
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
baseSettings.edgeFnDownloadURL
)
expect(await sandboxSettings.processSignal).toEqual(edgeFnResponseBody)
})

test('normalizes edgeFnDownloadURL when functionHost is provided', async () => {
const settings: SandboxSettingsConfig = {
...baseSettings,
processSignal: undefined,
functionHost: 'newHost.com',
edgeFnDownloadURL: 'https://original.com/download',
}
new SandboxSettings(settings)
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
'https://newHost.com/download'
)
})

test('creates default processSignal when parameters are missing', async () => {
const consoleWarnSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => {})
const settings: SandboxSettingsConfig = {
...baseSettings,
processSignal: undefined,
edgeFnDownloadURL: undefined,
}
const sandboxSettings = new SandboxSettings(settings)
expect(await sandboxSettings.processSignal).toEqual(
'globalThis.processSignal = function processSignal() {}'
)
expect(baseSettings.edgeFnFetchClient).not.toHaveBeenCalled()
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('processSignal')
)
})
})
4 changes: 3 additions & 1 deletion packages/signals/signals/src/core/processor/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export class SandboxSettings {
this.processSignal = Promise.resolve(
`globalThis.processSignal = function processSignal() {}`
)
console.warn('edgeFnDownloadURL or processSignal must be defined')
console.warn(
`No processSignal function found. Have you written a processSignal function on app.segment.com?`
)
return
}

Expand Down

0 comments on commit 11ca952

Please sign in to comment.