subagent-child-navigation.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { base64Encode } from "@opencode-ai/core/util/encode"
  2. import { expect, test, type Page } from "@playwright/test"
  3. import { currentSession, mockOpenCodeServer } from "../utils/mock-server"
  4. import { expectSessionTitle } from "../utils/waits"
  5. const directory = "C:/OpenCode/SubagentNavigation"
  6. const projectID = "proj_subagent_navigation"
  7. const parentID = "ses_subagent_parent"
  8. const childID = "ses_subagent_child"
  9. const parentTitle = "Parent session"
  10. const childTitle = "Subagent child session"
  11. // Child session pages derive their heading from the task part that spawned them.
  12. const taskDescription = "Inspect child navigation"
  13. type EventPayload = { directory: string; payload: Record<string, unknown> }
  14. test.use({ viewport: { width: 1440, height: 900 } })
  15. test("navigates to a subagent child session missing from the session list", async ({ page }) => {
  16. await setup(page)
  17. await openChildFromParent(page)
  18. await expectSessionTitle(page, taskDescription)
  19. await expect(page.getByRole("heading", { name: parentTitle })).toHaveCount(0)
  20. const titlebarRight = page.locator("#opencode-titlebar-right")
  21. await expect(titlebarRight.getByRole("button", { name: "Toggle review" })).toHaveCount(1)
  22. })
  23. test("shows the not found fallback when the viewed session is deleted", async ({ page }) => {
  24. const events: EventPayload[] = []
  25. await setup(page, () => events.splice(0, 1))
  26. await openChildFromParent(page)
  27. await expectSessionTitle(page, taskDescription)
  28. events.push({
  29. directory,
  30. payload: { type: "session.deleted", properties: { info: childSession() } },
  31. })
  32. await expect(page.getByText("This session cannot be found")).toBeVisible()
  33. await expect(page.getByRole("button", { name: "Close Tab" })).toBeVisible()
  34. await expect(page.getByRole("heading", { name: taskDescription })).toHaveCount(0)
  35. })
  36. async function setup(page: Page, events?: () => EventPayload[]) {
  37. await mockOpenCodeServer(page, {
  38. directory,
  39. project: {
  40. id: projectID,
  41. worktree: directory,
  42. vcs: "git",
  43. name: "subagent-navigation",
  44. time: { created: 1700000000000, updated: 1700000000000 },
  45. sandboxes: [],
  46. },
  47. provider: {
  48. all: [
  49. {
  50. id: "opencode",
  51. name: "OpenCode",
  52. models: {
  53. "claude-opus-4-6": { id: "claude-opus-4-6", name: "Claude Opus 4.6", limit: { context: 200_000 } },
  54. },
  55. },
  56. ],
  57. connected: ["opencode"],
  58. default: { providerID: "opencode", modelID: "claude-opus-4-6" },
  59. },
  60. sessions: [session(parentID, parentTitle, 1700000000000), childSession()],
  61. pageMessages: (sessionID) => ({ items: sessionID === parentID ? parentMessages() : [] }),
  62. events,
  63. eventRetry: events ? 16 : undefined,
  64. })
  65. // The child session resolves by ID but is absent from the session list,
  66. // matching a subagent session that has not been loaded into the list cache yet.
  67. await page.route(
  68. (url) => url.pathname === "/api/session" && url.port === (process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"),
  69. (route) =>
  70. route.fulfill({
  71. status: 200,
  72. contentType: "application/json",
  73. headers: { "access-control-allow-origin": "*" },
  74. body: JSON.stringify({
  75. data: [currentSession(session(parentID, parentTitle, 1700000000000))],
  76. cursor: {},
  77. }),
  78. }),
  79. )
  80. await configurePage(page)
  81. }
  82. async function openChildFromParent(page: Page) {
  83. await page.goto(sessionHref(parentID))
  84. await expectSessionTitle(page, parentTitle)
  85. const card = page.locator(`a[href="${sessionHref(childID)}"]`)
  86. await expect(card).toBeVisible()
  87. await card.click()
  88. await expect(page).toHaveURL(new RegExp(`/server/.+/session/${childID}$`), { timeout: 15_000 })
  89. }
  90. function session(id: string, title: string, created: number, extra?: Record<string, unknown>) {
  91. return {
  92. id,
  93. slug: id,
  94. projectID,
  95. directory,
  96. title,
  97. version: "dev",
  98. time: { created, updated: created },
  99. ...extra,
  100. }
  101. }
  102. function childSession() {
  103. return session(childID, childTitle, 1700000001000, { parentID })
  104. }
  105. function parentMessages() {
  106. const userID = "msg_user_0001"
  107. const assistantID = "msg_assistant_0001"
  108. return [
  109. {
  110. info: {
  111. id: userID,
  112. sessionID: parentID,
  113. role: "user",
  114. time: { created: 1700000000000 },
  115. agent: "build",
  116. model: { providerID: "opencode", modelID: "claude-opus-4-6" },
  117. },
  118. parts: [
  119. {
  120. id: "prt_user_text_0001",
  121. sessionID: parentID,
  122. messageID: userID,
  123. type: "text",
  124. text: "Delegate work to a subagent",
  125. },
  126. ],
  127. },
  128. {
  129. info: {
  130. id: assistantID,
  131. sessionID: parentID,
  132. role: "assistant",
  133. time: { created: 1700000001000, completed: 1700000002000 },
  134. parentID: userID,
  135. modelID: "claude-opus-4-6",
  136. providerID: "opencode",
  137. mode: "build",
  138. agent: "build",
  139. path: { cwd: directory, root: directory },
  140. cost: 0.01,
  141. tokens: { input: 100, output: 200, reasoning: 0, cache: { read: 0, write: 0 } },
  142. finish: "stop",
  143. },
  144. parts: [
  145. {
  146. id: "prt_tool_task_0001",
  147. sessionID: parentID,
  148. messageID: assistantID,
  149. type: "tool",
  150. callID: "call_task_0001",
  151. tool: "task",
  152. state: {
  153. status: "completed",
  154. input: { description: taskDescription, subagent_type: "explore" },
  155. output: "Subagent finished",
  156. title: taskDescription,
  157. metadata: { sessionId: childID },
  158. time: { start: 1700000001000, end: 1700000002000 },
  159. },
  160. },
  161. ],
  162. },
  163. ]
  164. }
  165. async function configurePage(page: Page) {
  166. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  167. await page.addInitScript(
  168. ({ directory, server, sessionId }) => {
  169. localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } }))
  170. localStorage.setItem(
  171. "opencode.global.dat:server",
  172. JSON.stringify({
  173. projects: { local: [{ worktree: directory, expanded: true }] },
  174. lastProject: { local: directory },
  175. }),
  176. )
  177. localStorage.setItem("opencode.window.browser.dat:tabs", JSON.stringify([{ type: "session", server, sessionId }]))
  178. },
  179. { directory, server, sessionId: parentID },
  180. )
  181. }
  182. function sessionHref(sessionID: string) {
  183. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  184. return `/server/${base64Encode(server)}/session/${sessionID}`
  185. }