server-scope.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "bun:test"
  2. import { ScopedKey, ServerScope, SessionRouteKey, SessionStateKey, migrateLegacySessionStateKeys } from "./server-scope"
  3. describe("ServerScope", () => {
  4. test("uses a stable local scope for the canonical sidecar", () => {
  5. expect(String(ServerScope.fromServerKey("sidecar" as Parameters<typeof ServerScope.fromServerKey>[0]))).toBe(
  6. "local",
  7. )
  8. })
  9. test("keeps configured loopback servers distinct from the canonical sidecar", () => {
  10. expect(
  11. String(ServerScope.fromServerKey("http://localhost:4096" as Parameters<typeof ServerScope.fromServerKey>[0])),
  12. ).toBe("http://localhost:4096")
  13. })
  14. test("uses a stable local scope for an explicit canonical web server", () => {
  15. const key = "http://localhost:4096" as Parameters<typeof ServerScope.fromServerKey>[0]
  16. expect(String(ServerScope.fromServerKey(key, key))).toBe("local")
  17. })
  18. })
  19. describe("SessionStateKey", () => {
  20. test("combines local and remote scope with route identity", () => {
  21. const route = SessionRouteKey.fromRoute("cmVwbw", "session-1")
  22. expect(String(SessionStateKey.from(ServerScope.local, route))).toBe("local\0cmVwbw/session-1")
  23. expect(String(SessionStateKey.from("https://windows.example" as ServerScope, route))).toBe(
  24. "https://windows.example\0cmVwbw/session-1",
  25. )
  26. expect(SessionStateKey.from("https://debian.example" as ServerScope, route)).not.toBe(
  27. SessionStateKey.from("https://windows.example" as ServerScope, route),
  28. )
  29. })
  30. test("extracts route keys from scoped and legacy state keys", () => {
  31. expect(String(SessionStateKey.route("cmVwbw/session-1"))).toBe("cmVwbw/session-1")
  32. expect(String(SessionStateKey.route("local\0cmVwbw/session-1"))).toBe("cmVwbw/session-1")
  33. expect(String(SessionStateKey.route("https://debian.example\0cmVwbw/session-1"))).toBe("cmVwbw/session-1")
  34. })
  35. })
  36. describe("migrateLegacySessionStateKeys", () => {
  37. test("copies legacy route keys into local scope without overwriting scoped state", () => {
  38. expect(
  39. migrateLegacySessionStateKeys({
  40. "cmVwbw/session-1": { active: "legacy" },
  41. "local\0cmVwbw/session-1": { active: "scoped" },
  42. "https://debian.example\0cmVwbw/session-1": { active: "remote" },
  43. }),
  44. ).toEqual({
  45. "local\0cmVwbw/session-1": { active: "scoped" },
  46. "https://debian.example\0cmVwbw/session-1": { active: "remote" },
  47. })
  48. })
  49. test("rejects invalid identity fragments", () => {
  50. expect(() => ScopedKey.from(ServerScope.local, "bad\0directory")).toThrow(
  51. "Scoped key part cannot contain null bytes",
  52. )
  53. })
  54. })