subagent-child-navigation.spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { base64Encode } from "@opencode-ai/core/util/encode"
  2. import { expect, test, type Page } from "@playwright/test"
  3. import { 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 via /session/: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 === "/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([session(parentID, parentTitle, 1700000000000)]),
  75. }),
  76. )
  77. await configurePage(page)
  78. }
  79. async function openChildFromParent(page: Page) {
  80. await page.goto(sessionHref(parentID))
  81. await expectSessionTitle(page, parentTitle)
  82. const card = page.locator(`a[href="${sessionHref(childID)}"]`)
  83. await expect(card).toBeVisible()
  84. await card.click()
  85. await expect(page).toHaveURL(new RegExp(`/server/.+/session/${childID}$`), { timeout: 15_000 })
  86. }
  87. function session(id: string, title: string, created: number, extra?: Record<string, unknown>) {
  88. return {
  89. id,
  90. slug: id,
  91. projectID,
  92. directory,
  93. title,
  94. version: "dev",
  95. time: { created, updated: created },
  96. ...extra,
  97. }
  98. }
  99. function childSession() {
  100. return session(childID, childTitle, 1700000001000, { parentID })
  101. }
  102. function parentMessages() {
  103. const userID = "msg_user_0001"
  104. const assistantID = "msg_assistant_0001"
  105. return [
  106. {
  107. info: {
  108. id: userID,
  109. sessionID: parentID,
  110. role: "user",
  111. time: { created: 1700000000000 },
  112. agent: "build",
  113. model: { providerID: "opencode", modelID: "claude-opus-4-6" },
  114. },
  115. parts: [
  116. {
  117. id: "prt_user_text_0001",
  118. sessionID: parentID,
  119. messageID: userID,
  120. type: "text",
  121. text: "Delegate work to a subagent",
  122. },
  123. ],
  124. },
  125. {
  126. info: {
  127. id: assistantID,
  128. sessionID: parentID,
  129. role: "assistant",
  130. time: { created: 1700000001000, completed: 1700000002000 },
  131. parentID: userID,
  132. modelID: "claude-opus-4-6",
  133. providerID: "opencode",
  134. mode: "build",
  135. agent: "build",
  136. path: { cwd: directory, root: directory },
  137. cost: 0.01,
  138. tokens: { input: 100, output: 200, reasoning: 0, cache: { read: 0, write: 0 } },
  139. finish: "stop",
  140. },
  141. parts: [
  142. {
  143. id: "prt_tool_task_0001",
  144. sessionID: parentID,
  145. messageID: assistantID,
  146. type: "tool",
  147. callID: "call_task_0001",
  148. tool: "task",
  149. state: {
  150. status: "completed",
  151. input: { description: taskDescription, subagent_type: "explore" },
  152. output: "Subagent finished",
  153. title: taskDescription,
  154. metadata: { sessionId: childID },
  155. time: { start: 1700000001000, end: 1700000002000 },
  156. },
  157. },
  158. ],
  159. },
  160. ]
  161. }
  162. async function configurePage(page: Page) {
  163. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  164. await page.addInitScript(
  165. ({ directory, server, sessionId }) => {
  166. localStorage.setItem("settings.v3", JSON.stringify({ general: { newLayoutDesigns: true } }))
  167. localStorage.setItem(
  168. "opencode.global.dat:server",
  169. JSON.stringify({
  170. projects: { local: [{ worktree: directory, expanded: true }] },
  171. lastProject: { local: directory },
  172. }),
  173. )
  174. localStorage.setItem("opencode.window.browser.dat:tabs", JSON.stringify([{ type: "session", server, sessionId }]))
  175. },
  176. { directory, server, sessionId: parentID },
  177. )
  178. }
  179. function sessionHref(sessionID: string) {
  180. const server = `http://${process.env.PLAYWRIGHT_SERVER_HOST ?? "127.0.0.1"}:${process.env.PLAYWRIGHT_SERVER_PORT ?? "4096"}`
  181. return `/server/${base64Encode(server)}/session/${sessionID}`
  182. }