terminal.test.ts 2.5 KB

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