11import { describe , expect , test } from "bun:test" ;
2+ import { PLAYBOOK_APP_METADATA_KEY , PLAYBOOK_METADATA_KEY } from "@openagentpack/playbooks" ;
3+ import type { ProviderSessionEvent , Session } from "@openagentpack/sdk" ;
24import {
35 createPlaybookSessionRuntime ,
4- PLAYBOOK_APP_METADATA_KEY ,
5- PLAYBOOK_METADATA_KEY ,
66 PlaybookAgentIdentityMismatchError ,
77 pickPlaybookAgent ,
88 type RemotePlaybookAgent ,
9- } from "../src/index.ts" ;
9+ } from "./runtime" ;
10+ import type { PlaybookSessionDetail } from "./sessions" ;
1011
1112const APP_ID = "agents-webui" ;
1213
1314function agent ( overrides : Partial < RemotePlaybookAgent > = { } ) : RemotePlaybookAgent {
1415 return {
1516 id : "agent_1" ,
16- name : "Agents/设计师助手 " ,
17+ name : "Agents/\u8BBE\u8BA1\u5E08\u52A9\u624B " ,
1718 metadata : {
1819 [ PLAYBOOK_APP_METADATA_KEY ] : APP_ID ,
1920 [ PLAYBOOK_METADATA_KEY ] : "designer" ,
@@ -23,6 +24,22 @@ function agent(overrides: Partial<RemotePlaybookAgent> = {}): RemotePlaybookAgen
2324 } ;
2425}
2526
27+ function fakeEvent ( raw_type = "created" ) : ProviderSessionEvent {
28+ return { type : "status" , raw_type, raw : { } } ;
29+ }
30+
31+ function fakeSession ( id : string , agentId ?: string ) : Session {
32+ return {
33+ session_id : id ,
34+ status : "running" ,
35+ agent : agentId ? { agent_id : agentId } : undefined ,
36+ } ;
37+ }
38+
39+ function fakeDetail ( sessionId : string , agentId ?: string ) : PlaybookSessionDetail {
40+ return { session : fakeSession ( sessionId , agentId ) , events : [ ] } ;
41+ }
42+
2643describe ( "pickPlaybookAgent" , ( ) => {
2744 test ( "picks the current-app playbook stamp by newest update time" , ( ) => {
2845 const older = agent ( { id : "agent_old" , updatedAt : "2026-06-20T00:00:00.000Z" } ) ;
@@ -43,7 +60,7 @@ describe("pickPlaybookAgent", () => {
4360 metadata : { [ PLAYBOOK_APP_METADATA_KEY ] : APP_ID } ,
4461 } ) ,
4562 ] ,
46- { playbookId : "designer" , appId : APP_ID , expectedAgentName : "Agents/设计师助手 " } ,
63+ { playbookId : "designer" , appId : APP_ID , expectedAgentName : "Agents/\u8BBE\u8BA1\u5E08\u52A9\u624B " } ,
4764 ) ;
4865
4966 expect ( pick . agent ) . toBeUndefined ( ) ;
@@ -64,10 +81,10 @@ describe("createPlaybookSessionRuntime", () => {
6481 test ( "start ensures the selected agent, starts the provider session, attaches events, then returns detail" , async ( ) => {
6582 const calls : string [ ] = [ ] ;
6683 const liveEvents = ( async function * ( ) {
67- yield { type : "created" } ;
84+ yield fakeEvent ( ) ;
6885 } ) ( ) ;
6986 const runtime = createPlaybookSessionRuntime ( {
70- identity : { appId : APP_ID , expectedAgentName : ( ) => "Agents/设计师助手 " } ,
87+ identity : { appId : APP_ID , expectedAgentName : ( ) => "Agents/\u8BBE\u8BA1\u5E08\u52A9\u624B " } ,
7188 agents : {
7289 async listPlaybookAgents ( ) {
7390 calls . push ( "agents.list" ) ;
@@ -94,7 +111,7 @@ describe("createPlaybookSessionRuntime", () => {
94111 } ,
95112 async getDetail ( input ) {
96113 calls . push ( `sessions.detail:${ input . sessionId } :${ input . remoteAgentId } ` ) ;
97- return { sessionId : input . sessionId , agentId : input . remoteAgentId } ;
114+ return fakeDetail ( input . sessionId , input . remoteAgentId ) ;
98115 } ,
99116 } ,
100117 events : {
@@ -114,7 +131,8 @@ describe("createPlaybookSessionRuntime", () => {
114131 model : "glm-5.1" ,
115132 } ) ;
116133
117- expect ( detail ) . toEqual ( { sessionId : "sess_1" , agentId : "agent_existing" } ) ;
134+ expect ( detail . session . session_id ) . toBe ( "sess_1" ) ;
135+ expect ( detail . session . agent ?. agent_id ) . toBe ( "agent_existing" ) ;
118136 expect ( calls ) . toEqual ( [
119137 "agents.list" ,
120138 "agents.ensure:agent_existing:glm-5.1" ,
@@ -126,7 +144,7 @@ describe("createPlaybookSessionRuntime", () => {
126144
127145 test ( "start throws before ensuring when identity is blocked" , async ( ) => {
128146 const runtime = createPlaybookSessionRuntime ( {
129- identity : { appId : APP_ID , expectedAgentName : ( ) => "Agents/设计师助手 " } ,
147+ identity : { appId : APP_ID , expectedAgentName : ( ) => "Agents/\u8BBE\u8BA1\u5E08\u52A9\u624B " } ,
130148 agents : {
131149 async listPlaybookAgents ( ) {
132150 return [ agent ( { metadata : { [ PLAYBOOK_APP_METADATA_KEY ] : APP_ID } } ) ] ;
@@ -162,7 +180,7 @@ describe("createPlaybookSessionRuntime", () => {
162180 test ( "send appends a message, attaches events, then returns detail" , async ( ) => {
163181 const calls : string [ ] = [ ] ;
164182 const liveEvents = ( async function * ( ) {
165- yield { type : "message" } ;
183+ yield fakeEvent ( "message" ) ;
166184 } ) ( ) ;
167185 const runtime = createPlaybookSessionRuntime ( {
168186 identity : { appId : APP_ID } ,
@@ -190,7 +208,7 @@ describe("createPlaybookSessionRuntime", () => {
190208 } ,
191209 async getDetail ( input ) {
192210 calls . push ( `sessions.detail:${ input . sessionId } :${ input . playbookId } ` ) ;
193- return { sessionId : input . sessionId , playbookId : input . playbookId } ;
211+ return fakeDetail ( input . sessionId ) ;
194212 } ,
195213 } ,
196214 events : {
@@ -205,7 +223,7 @@ describe("createPlaybookSessionRuntime", () => {
205223
206224 const detail = await runtime . send ( { playbookId : "designer" , sessionId : "sess_1" , message : "continue" } ) ;
207225
208- expect ( detail ) . toEqual ( { sessionId : "sess_1" , playbookId : "designer" } ) ;
226+ expect ( detail . session . session_id ) . toBe ( "sess_1" ) ;
209227 expect ( calls ) . toEqual ( [
210228 "sessions.send:sess_1:designer:continue" ,
211229 "events.attach:sess_1" ,
@@ -228,7 +246,7 @@ describe("createPlaybookSessionRuntime", () => {
228246 sessions : {
229247 async list ( input ) {
230248 calls . push ( `sessions.list:${ input . playbookId } :${ input . remoteAgentId } :${ input . limit } :${ input . pageToken } ` ) ;
231- return { sessions : [ { id : "sess_1" } ] , nextPageToken : "next" } ;
249+ return { sessions : [ fakeSession ( "sess_1" ) ] , nextPageToken : "next" } ;
232250 } ,
233251 async start ( ) {
234252 throw new Error ( "should not start" ) ;
@@ -252,7 +270,8 @@ describe("createPlaybookSessionRuntime", () => {
252270 pageToken : "page_1" ,
253271 } ) ;
254272
255- expect ( listed ) . toEqual ( { sessions : [ { id : "sess_1" } ] , nextPageToken : "next" } ) ;
273+ expect ( listed . sessions [ 0 ] ?. session_id ) . toBe ( "sess_1" ) ;
274+ expect ( listed . nextPageToken ) . toBe ( "next" ) ;
256275 expect ( calls ) . toEqual ( [ "sessions.list:designer:agent_1:10:page_1" ] ) ;
257276 } ) ;
258277
@@ -283,14 +302,14 @@ describe("createPlaybookSessionRuntime", () => {
283302 } ,
284303 async getDetail ( input ) {
285304 calls . push ( `sessions.detail:${ input . sessionId } :${ input . playbookId } ` ) ;
286- return { sessionId : input . sessionId , playbookId : input . playbookId } ;
305+ return fakeDetail ( input . sessionId ) ;
287306 } ,
288307 } ,
289308 } ) ;
290309
291310 const detail = await runtime . getDetail ( { playbookId : "designer" , sessionId : "sess_1" } ) ;
292311
293- expect ( detail ) . toEqual ( { sessionId : "sess_1" , playbookId : "designer" } ) ;
312+ expect ( detail . session . session_id ) . toBe ( "sess_1" ) ;
294313 expect ( calls ) . toEqual ( [ "sessions.detail:sess_1:designer" ] ) ;
295314 } ) ;
296315
0 commit comments