terminal.test.ts 2.8 KB

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