terminal.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { beforeAll, describe, expect, mock, test } from "bun:test"
  2. import { ServerScope } from "@/utils/server-scope"
  3. let getWorkspaceTerminalCacheKey: typeof import("./terminal").getWorkspaceTerminalCacheKey
  4. let getLegacyTerminalStorageKeys: (dir: string, legacySessionID?: string) => string[]
  5. let migrateTerminalState: (value: unknown) => unknown
  6. beforeAll(async () => {
  7. mock.module("@solidjs/router", () => ({
  8. useNavigate: () => () => undefined,
  9. useParams: () => ({}),
  10. }))
  11. mock.module("@opencode-ai/ui/context", () => ({
  12. createSimpleContext: () => ({
  13. use: () => undefined,
  14. provider: () => undefined,
  15. }),
  16. }))
  17. const mod = await import("./terminal")
  18. getWorkspaceTerminalCacheKey = mod.getWorkspaceTerminalCacheKey
  19. getLegacyTerminalStorageKeys = mod.getLegacyTerminalStorageKeys
  20. migrateTerminalState = mod.migrateTerminalState
  21. })
  22. describe("getWorkspaceTerminalCacheKey", () => {
  23. test("uses workspace-only directory cache key", () => {
  24. expect(String(getWorkspaceTerminalCacheKey("/repo"))).toBe("local\u0000/repo\u0000__workspace__")
  25. })
  26. test("can include a server scope", () => {
  27. expect(String(getWorkspaceTerminalCacheKey("/repo", "ssh:debian" as ServerScope))).toBe(
  28. "ssh:debian\u0000/repo\u0000__workspace__",
  29. )
  30. })
  31. })
  32. describe("getLegacyTerminalStorageKeys", () => {
  33. test("keeps workspace storage path when no legacy session id", () => {
  34. expect(getLegacyTerminalStorageKeys("/repo")).toEqual(["/repo/terminal.v1"])
  35. })
  36. test("includes legacy session path before workspace path", () => {
  37. expect(getLegacyTerminalStorageKeys("/repo", "session-123")).toEqual([
  38. "/repo/terminal/session-123.v1",
  39. "/repo/terminal.v1",
  40. ])
  41. })
  42. })
  43. describe("migrateTerminalState", () => {
  44. test("drops invalid terminals and restores a valid active terminal", () => {
  45. expect(
  46. migrateTerminalState({
  47. active: "missing",
  48. all: [
  49. null,
  50. { id: "one", title: "Terminal 2" },
  51. { id: "one", title: "duplicate", titleNumber: 9 },
  52. { id: "two", title: "logs", titleNumber: 4, rows: 24, cols: 80 },
  53. { title: "no-id" },
  54. ],
  55. }),
  56. ).toEqual({
  57. active: "one",
  58. all: [
  59. { id: "one", title: "Terminal 2", titleNumber: 2 },
  60. { id: "two", title: "logs", titleNumber: 4, rows: 24, cols: 80 },
  61. ],
  62. })
  63. })
  64. test("keeps a valid active id", () => {
  65. expect(
  66. migrateTerminalState({
  67. active: "two",
  68. all: [
  69. { id: "one", title: "Terminal 1" },
  70. { id: "two", title: "shell", titleNumber: 7 },
  71. ],
  72. }),
  73. ).toEqual({
  74. active: "two",
  75. all: [
  76. { id: "one", title: "Terminal 1", titleNumber: 1 },
  77. { id: "two", title: "shell", titleNumber: 7 },
  78. ],
  79. })
  80. })
  81. })