11// Canvas tests cover index plugin behavior.
2- import type { AnyAgentTool , OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry" ;
2+ import type {
3+ AnyAgentTool ,
4+ OpenClawPluginApi ,
5+ OpenClawPluginNodeInvokePolicyContext ,
6+ } from "openclaw/plugin-sdk/plugin-entry" ;
37import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api" ;
48import { beforeEach , describe , expect , it , vi } from "vitest" ;
59import canvasPlugin from "./index.js" ;
610import { SHOW_WIDGET_REQUIRED_CLIENT_CAPS } from "./src/tool-schema.js" ;
711
12+ const VALID_A2UI_V08_JSONL = [
13+ JSON . stringify ( {
14+ surfaceUpdate : {
15+ surfaceId : "main" ,
16+ components : [
17+ {
18+ id : "root" ,
19+ component : { Text : { text : { literalString : "Canvas proof" } , usageHint : "body" } } ,
20+ } ,
21+ ] ,
22+ } ,
23+ } ) ,
24+ JSON . stringify ( { beginRendering : { surfaceId : "main" , root : "root" } } ) ,
25+ ] . join ( "\n" ) ;
26+
827const mocks = vi . hoisted ( ( ) => {
928 const httpHandler = {
1029 handleHttpRequest : vi . fn ( async ( ) => true ) ,
@@ -71,6 +90,8 @@ function registerCanvas() {
7190 registrar : Parameters < OpenClawPluginApi [ "registerNodeCliFeature" ] > [ 0 ] ;
7291 opts : Parameters < OpenClawPluginApi [ "registerNodeCliFeature" ] > [ 1 ] ;
7392 } > = [ ] ;
93+ const nodeInvokePolicies : Array < Parameters < OpenClawPluginApi [ "registerNodeInvokePolicy" ] > [ 0 ] > =
94+ [ ] ;
7495 canvasPlugin . register ?.(
7596 createTestPluginApi ( {
7697 id : "canvas" ,
@@ -81,10 +102,23 @@ function registerCanvas() {
81102 registerHostedMediaResolver : ( resolver ) => resolvers . push ( resolver ) ,
82103 registerTool : ( tool , opts ) => tools . push ( { tool, opts } ) ,
83104 registerNodeCliFeature : ( registrar , opts ) => cliFeatures . push ( { registrar, opts } ) ,
84- registerNodeInvokePolicy : vi . fn ( ) ,
105+ registerNodeInvokePolicy : ( policy ) => nodeInvokePolicies . push ( policy ) ,
85106 } ) ,
86107 ) ;
87- return { routes, services, resolvers, tools, cliFeatures } ;
108+ return { routes, services, resolvers, tools, cliFeatures, nodeInvokePolicies } ;
109+ }
110+
111+ function createNodeInvokeContext (
112+ params : Partial < OpenClawPluginNodeInvokePolicyContext > ,
113+ ) : OpenClawPluginNodeInvokePolicyContext {
114+ return {
115+ nodeId : "node-1" ,
116+ command : "canvas.a2ui.pushJSONL" ,
117+ params : { } ,
118+ config : { } ,
119+ invokeNode : vi . fn ( async ( ) => ( { ok : true as const } ) ) ,
120+ ...params ,
121+ } ;
88122}
89123
90124describe ( "Canvas plugin entry" , ( ) => {
@@ -175,4 +209,72 @@ describe("Canvas plugin entry", () => {
175209 widget_code : "<p>ready</p>" ,
176210 } ) ;
177211 } ) ;
212+
213+ it . each ( [
214+ [ "malformed pushJSONL" , "canvas.a2ui.pushJSONL" , { jsonl : "{not-json}" } ] ,
215+ [
216+ "versioned A2UI v0.9 pushJSONL" ,
217+ "canvas.a2ui.pushJSONL" ,
218+ {
219+ jsonl : JSON . stringify ( { version : "v0.9" , deleteSurface : { surfaceId : "main" } } ) ,
220+ } ,
221+ ] ,
222+ [ "malformed legacy push JSONL fallback" , "canvas.a2ui.push" , { jsonl : "{not-json}" } ] ,
223+ ] ) ( "rejects %s at the final Canvas node policy" , async ( _label , command , params ) => {
224+ const { nodeInvokePolicies } = registerCanvas ( ) ;
225+ const policy = nodeInvokePolicies [ 0 ] ;
226+ if ( ! policy ) {
227+ throw new Error ( "Canvas node invoke policy was not registered" ) ;
228+ }
229+ const invokeNode = vi . fn ( async ( ) => ( { ok : true as const } ) ) ;
230+
231+ const result = await policy . handle ( createNodeInvokeContext ( { command, params, invokeNode } ) ) ;
232+
233+ expect ( result ) . toMatchObject ( { ok : false , code : "INVALID_A2UI_JSONL" } ) ;
234+ expect ( invokeNode ) . not . toHaveBeenCalled ( ) ;
235+ } ) ;
236+
237+ it ( "dispatches A2UI v0.8 JSONL unchanged through the final Canvas node policy" , async ( ) => {
238+ const { nodeInvokePolicies } = registerCanvas ( ) ;
239+ const policy = nodeInvokePolicies [ 0 ] ;
240+ if ( ! policy ) {
241+ throw new Error ( "Canvas node invoke policy was not registered" ) ;
242+ }
243+ const invokeNode = vi . fn ( async ( ) => ( { ok : true as const } ) ) ;
244+
245+ const result = await policy . handle (
246+ createNodeInvokeContext ( {
247+ params : { jsonl : VALID_A2UI_V08_JSONL } ,
248+ invokeNode,
249+ } ) ,
250+ ) ;
251+
252+ expect ( result ) . toEqual ( { ok : true } ) ;
253+ expect ( invokeNode ) . toHaveBeenCalledTimes ( 1 ) ;
254+ expect ( invokeNode ) . toHaveBeenCalledWith ( ) ;
255+ } ) ;
256+
257+ it ( "leaves canonical A2UI message arrays to the native node" , async ( ) => {
258+ const { nodeInvokePolicies } = registerCanvas ( ) ;
259+ const policy = nodeInvokePolicies [ 0 ] ;
260+ if ( ! policy ) {
261+ throw new Error ( "Canvas node invoke policy was not registered" ) ;
262+ }
263+ const invokeNode = vi . fn ( async ( ) => ( { ok : true as const } ) ) ;
264+
265+ const result = await policy . handle (
266+ createNodeInvokeContext ( {
267+ command : "canvas.a2ui.push" ,
268+ params : {
269+ messages : [ { deleteSurface : { surfaceId : "main" } } ] ,
270+ jsonl : "{not-used-by-native-node}" ,
271+ } ,
272+ invokeNode,
273+ } ) ,
274+ ) ;
275+
276+ expect ( result ) . toEqual ( { ok : true } ) ;
277+ expect ( invokeNode ) . toHaveBeenCalledTimes ( 1 ) ;
278+ expect ( invokeNode ) . toHaveBeenCalledWith ( ) ;
279+ } ) ;
178280} ) ;
0 commit comments