Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bridge/src/bridge-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export declare interface BridgeMessage {
type: PreviewBridgeMessageType;
/** Optional payload data associated with the message event. */
payload?: unknown;
/** Optional host-assigned dispatch identifier echoed on RENDER_SUCCESS / RENDER_ERROR. */
renderId?: string;
/** Capability flag announced on RENDERER_READY when the bridge echoes renderId. */
supportsRenderId?: boolean;
/** Extensible custom properties. */
[key: string]: unknown;
}
Expand Down
257 changes: 255 additions & 2 deletions bridge/src/preview-bridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('PreviewBridge Core API Runtime', () => {
expect(bridge['activeRenderer']).toBeNull();
});

it('emits RENDERER_READY signal only when attachRenderer is called', async () => {
it('emits RENDERER_READY signal with supportsRenderId capability when attachRenderer is called', async () => {
const spy = vi.spyOn(window.parent, 'postMessage');

// Handshake should NOT be sent upon simple document load events anymore
Expand All @@ -165,7 +165,10 @@ describe('PreviewBridge Core API Runtime', () => {
});

expect(spy).toHaveBeenCalledWith(
expect.objectContaining({type: PreviewBridgeMessageType.RENDERER_READY}),
expect.objectContaining({
type: PreviewBridgeMessageType.RENDERER_READY,
supportsRenderId: true,
}),
window.location.origin,
);
});
Expand Down Expand Up @@ -2987,6 +2990,256 @@ describe('PreviewBridge Core API Runtime', () => {
expect.anything(),
);
});

it('echoes renderId on RENDER_SUCCESS and RENDER_ERROR when provided, and omits it when untagged', async () => {
const postSpy = vi.mocked(window.parent.postMessage);
postSpy.mockClear();

window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-tagged-1',
payload: [{version: 'v0.9', updateComponents: {surfaceId: 's1', components: []}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

expect(postSpy).toHaveBeenCalledWith(
{
type: PreviewBridgeMessageType.RENDER_SUCCESS,
renderId: 'render-tagged-1',
},
expect.anything(),
);

postSpy.mockClear();
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
payload: [{version: 'v0.9', updateComponents: {surfaceId: 's1', components: []}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

const untaggedCall = postSpy.mock.calls.find(
c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS,
);
expect(untaggedCall?.[0]).toEqual({type: PreviewBridgeMessageType.RENDER_SUCCESS});

postSpy.mockClear();
processMessagesMock.mockImplementation(() => {
throw new Error('Render failed');
});
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-err-1',
payload: [{version: 'v0.9', updateComponents: {surfaceId: 's1', components: []}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

expect(postSpy).toHaveBeenCalledWith(
expect.objectContaining({
type: PreviewBridgeMessageType.RENDER_ERROR,
renderId: 'render-err-1',
}),
expect.anything(),
);
});

it('coalesces overlapping createSurface dispatches and echoes only the latest renderId without clobbering synchronous sends', async () => {
vi.useFakeTimers();
try {
const postSpy = vi.mocked(window.parent.postMessage);
postSpy.mockClear();

window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-coalesced-1',
payload: [{version: 'v0.9', createSurface: {surfaceId: 's1', catalogId: 'c1'}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-coalesced-2',
payload: [{version: 'v0.9', createSurface: {surfaceId: 's1', catalogId: 'c1'}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

// Non-createSurface message while a createSurface timer is pending queues behind it
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
payload: [
{
version: '1.0',
callRendererFunction: {
surfaceId: 's1',
functionCallId: 'fn-1',
callFunction: {'@call': 'ping'},
},
},
],
},
source: window.parent,
origin: window.location.origin,
}),
);

expect(
postSpy.mock.calls.filter(
c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS,
),
).toHaveLength(0);

vi.runAllTimers();

const deferredSuccess = postSpy.mock.calls
.filter(c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS)
.map(c => c[0]);
expect(deferredSuccess).toEqual([
{type: PreviewBridgeMessageType.RENDER_SUCCESS, renderId: 'render-coalesced-2'},
{type: PreviewBridgeMessageType.RENDER_SUCCESS},
]);
} finally {
vi.useRealTimers();
}
});

it('executes deferred follow-up updates after a pending createSurface in FIFO order and discards them if superseded by a newer createSurface', () => {
vi.useFakeTimers();
try {
const postSpy = vi.mocked(window.parent.postMessage);
postSpy.mockClear();
processMessagesMock.mockClear();

// P1: createSurface (render-1) schedules deferred timer
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-1',
payload: [{version: 'v0.9', createSurface: {surfaceId: 's1', catalogId: 'c1'}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

// P2: incremental update (render-2) arrives before P1's timer fires -> must run AFTER P1
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-2',
payload: [{version: 'v0.9', updateDataModel: {surfaceId: 's1', value: {step: 2}}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

// Neither P1 nor P2 should have emitted RENDER_SUCCESS before timer flush
expect(
postSpy.mock.calls.filter(
c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS,
),
).toHaveLength(0);

vi.runAllTimers();

const successMsgs = postSpy.mock.calls
.filter(c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS)
.map(c => c[0]);
expect(successMsgs).toEqual([
{type: PreviewBridgeMessageType.RENDER_SUCCESS, renderId: 'render-1'},
{type: PreviewBridgeMessageType.RENDER_SUCCESS, renderId: 'render-2'},
]);

// Verify execution order in processMessages: P1 createSurface ran before P2 updateDataModel
const createIdx = processMessagesMock.mock.calls.findIndex(
call =>
Array.isArray(call[0]) &&
call[0].some((item: Record<string, unknown>) => 'createSurface' in item),
);
const updateIdx = processMessagesMock.mock.calls.findIndex(
call =>
Array.isArray(call[0]) &&
call[0].some((item: Record<string, unknown>) => 'updateDataModel' in item),
);
expect(createIdx).toBeGreaterThanOrEqual(0);
expect(updateIdx).toBeGreaterThan(createIdx);

// Now verify a newer createSurface discards a pending createSurface and its queued follow-ups
postSpy.mockClear();
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-old-1',
payload: [{version: 'v0.9', createSurface: {surfaceId: 's1', catalogId: 'c1'}}],
},
source: window.parent,
origin: window.location.origin,
}),
);
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-old-2',
payload: [{version: 'v0.9', updateDataModel: {surfaceId: 's1', value: {step: 99}}}],
},
source: window.parent,
origin: window.location.origin,
}),
);
window.dispatchEvent(
new MessageEvent('message', {
data: {
type: PreviewBridgeMessageType.RENDER_A2UI,
renderId: 'render-new-3',
payload: [{version: 'v0.9', createSurface: {surfaceId: 's1', catalogId: 'c1'}}],
},
source: window.parent,
origin: window.location.origin,
}),
);

vi.runAllTimers();

const supersedingSuccess = postSpy.mock.calls
.filter(c => (c[0] as {type?: string})?.type === PreviewBridgeMessageType.RENDER_SUCCESS)
.map(c => c[0]);
expect(supersedingSuccess).toEqual([
{type: PreviewBridgeMessageType.RENDER_SUCCESS, renderId: 'render-new-3'},
]);
} finally {
vi.useRealTimers();
}
});
});

describe('Outbound RPC Channel and Processor Management', () => {
Expand Down
Loading
Loading