55 lines
2.4 KiB
TypeScript
55 lines
2.4 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
import { PiExtensionUiProjector } from '../../electron/coding-runtime/pi/extension-ui-projector';
|
|
|
|
describe('Pi extension UI projector', () => {
|
|
it('projects registered UI safely and refuses stale editor revisions', () => {
|
|
let draftRevision = 4;
|
|
const projector = new PiExtensionUiProjector({
|
|
getDraftRevision: () => draftRevision,
|
|
knownWidgetKeys: ['makelore.runtime'],
|
|
});
|
|
projector.beginRun('conversation-a', 1, 'run-1');
|
|
expect(projector.project('conversation-a', 1, 'run-1', {
|
|
type: 'extension_ui_request', id: 'status', method: 'setStatus',
|
|
statusKey: 'makelore.write-lease', statusText: '等待项目写入',
|
|
})).toMatchObject({ kind: 'status', key: 'makelore.write-lease' });
|
|
expect(projector.project('conversation-a', 1, 'run-1', {
|
|
type: 'extension_ui_request', id: 'draft', method: 'set_editor_text', text: 'new text',
|
|
})).toEqual({
|
|
kind: 'editor-text', conversationId: 'conversation-a', text: 'new text', draftRevision: 4,
|
|
});
|
|
|
|
draftRevision = 5;
|
|
expect(projector.project('conversation-a', 1, 'run-1', {
|
|
type: 'extension_ui_request', id: 'stale-draft', method: 'set_editor_text', text: 'overwrite',
|
|
})).toBeNull();
|
|
expect(projector.getDiagnostics()).toContainEqual({
|
|
method: 'set_editor_text', reason: 'stale-draft-revision',
|
|
});
|
|
});
|
|
|
|
it('records bounded diagnostics instead of projecting unknown widget payloads', () => {
|
|
const projector = new PiExtensionUiProjector({ getDraftRevision: () => 0 });
|
|
projector.beginRun('conversation-a', 1, 'run-1');
|
|
expect(projector.project('conversation-a', 1, 'run-1', {
|
|
type: 'extension_ui_request', id: 'unknown', method: 'setWidget',
|
|
widgetKey: 'third-party-widget', widgetLines: ['raw detail'],
|
|
})).toBeNull();
|
|
expect(projector.getDiagnostics()).toEqual([{
|
|
method: 'setWidget', reason: 'unsupported-ui-method',
|
|
}]);
|
|
for (let index = 0; index < 1_000; index += 1) {
|
|
projector.project('conversation-a', 1, 'run-1', {
|
|
type: 'extension_ui_request', id: `unknown-${index}`, method: `unknown-${index}`,
|
|
});
|
|
}
|
|
const diagnostics = projector.getDiagnostics();
|
|
expect(diagnostics).toHaveLength(256);
|
|
expect(diagnostics.at(-1)).toEqual({
|
|
method: 'unknown-999', reason: 'invalid-ui-payload',
|
|
});
|
|
});
|
|
});
|