layout-tabs.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, expect, test } from "bun:test"
  2. import {
  3. SESSION_OPEN_FILE_TAB,
  4. closeSessionTab,
  5. openSessionTab,
  6. previewSessionTab,
  7. type SessionTabState,
  8. } from "./layout-tabs"
  9. const state = (all: string[], active?: string, preview?: string): SessionTabState => ({
  10. tabs: { all, active },
  11. preview,
  12. })
  13. describe("previewSessionTab", () => {
  14. test("appends the Open File placeholder", () => {
  15. expect(previewSessionTab(state(["file://a.ts"], "file://a.ts"), SESSION_OPEN_FILE_TAB)).toEqual(
  16. state(["file://a.ts", SESSION_OPEN_FILE_TAB], SESSION_OPEN_FILE_TAB, SESSION_OPEN_FILE_TAB),
  17. )
  18. })
  19. test("replaces the current preview in place", () => {
  20. expect(
  21. previewSessionTab(
  22. state(["context", SESSION_OPEN_FILE_TAB, "file://b.ts"], SESSION_OPEN_FILE_TAB, SESSION_OPEN_FILE_TAB),
  23. "file://a.ts",
  24. ),
  25. ).toEqual(state(["context", "file://a.ts", "file://b.ts"], "file://a.ts", "file://a.ts"))
  26. })
  27. test("activates a durable tab without duplicating it", () => {
  28. expect(
  29. previewSessionTab(
  30. state(["file://a.ts", SESSION_OPEN_FILE_TAB, "file://b.ts"], SESSION_OPEN_FILE_TAB, SESSION_OPEN_FILE_TAB),
  31. "file://b.ts",
  32. ),
  33. ).toEqual(state(["file://a.ts", "file://b.ts"], "file://b.ts"))
  34. })
  35. test("replaces a restored Open File placeholder", () => {
  36. expect(
  37. previewSessionTab(state(["file://a.ts", SESSION_OPEN_FILE_TAB], SESSION_OPEN_FILE_TAB), "file://b.ts"),
  38. ).toEqual(state(["file://a.ts", "file://b.ts"], "file://b.ts", "file://b.ts"))
  39. })
  40. })
  41. describe("openSessionTab", () => {
  42. test("pins the current preview", () => {
  43. expect(openSessionTab(state(["file://a.ts"], "file://a.ts", "file://a.ts"), "file://a.ts")).toEqual(
  44. state(["file://a.ts"], "file://a.ts"),
  45. )
  46. })
  47. test("replaces a preview with a directly opened file", () => {
  48. expect(openSessionTab(state(["file://a.ts"], "file://a.ts", "file://a.ts"), "file://b.ts")).toEqual(
  49. state(["file://b.ts"], "file://b.ts"),
  50. )
  51. })
  52. test("keeps the preview when switching to Review", () => {
  53. expect(openSessionTab(state(["file://a.ts"], "file://a.ts", "file://a.ts"), "review")).toEqual(
  54. state(["file://a.ts"], "review", "file://a.ts"),
  55. )
  56. })
  57. test("replaces a restored Open File placeholder with a direct open", () => {
  58. expect(openSessionTab(state(["file://a.ts", SESSION_OPEN_FILE_TAB], SESSION_OPEN_FILE_TAB), "file://b.ts")).toEqual(
  59. state(["file://a.ts", "file://b.ts"], "file://b.ts"),
  60. )
  61. })
  62. })
  63. describe("closeSessionTab", () => {
  64. test("clears preview metadata and selects the left neighbor", () => {
  65. expect(
  66. closeSessionTab(
  67. state(["file://a.ts", "file://b.ts", "file://c.ts"], "file://b.ts", "file://b.ts"),
  68. "file://b.ts",
  69. ),
  70. ).toEqual(state(["file://a.ts", "file://c.ts"], "file://a.ts"))
  71. })
  72. })