initialization.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import { initializationData, initializationReady } from "./initialization"
  3. describe("desktop renderer initialization", () => {
  4. test("throws the original initialization error before rendering server providers", () => {
  5. const error = new Error("sidecar startup failed")
  6. try {
  7. initializationData(Object.assign(() => undefined, { error }))
  8. throw new Error("expected initialization to fail")
  9. } catch (failure) {
  10. expect(failure).toBe(error)
  11. expect((failure as Error & { localServerStartup?: boolean }).localServerStartup).toBe(true)
  12. }
  13. })
  14. test("removes Electron's remote invocation wrapper from startup errors", () => {
  15. const error = new Error(
  16. "Error invoking remote method 'await-initialization': Error: Cannot migrate session_message projections",
  17. )
  18. try {
  19. initializationData(Object.assign(() => undefined, { error }))
  20. throw new Error("expected initialization to fail")
  21. } catch (failure) {
  22. expect(failure).toBe(error)
  23. expect((failure as Error).message).toBe("Cannot migrate session_message projections")
  24. }
  25. })
  26. test("returns initialized sidecar data", () => {
  27. const sidecar = { url: "http://127.0.0.1:1234", username: "opencode", password: "secret" }
  28. expect(initializationData(Object.assign(() => sidecar, { error: undefined }))).toBe(sidecar)
  29. })
  30. test("does not discard falsy initialization errors", () => {
  31. let caught: unknown
  32. try {
  33. initializationData(Object.assign(() => undefined, { error: "" }))
  34. } catch (error) {
  35. caught = error
  36. }
  37. expect(caught).toBeInstanceOf(Error)
  38. if (!(caught instanceof Error)) return
  39. expect(caught.message).toBe("")
  40. expect((caught as Error & { localServerStartup?: boolean }).localServerStartup).toBe(true)
  41. })
  42. test("checks initialization errors before rendering server providers", () => {
  43. const error = new Error("sidecar startup failed")
  44. expect(() => initializationReady(Object.assign(() => undefined, { error, loading: false }))).toThrow(error)
  45. })
  46. test("waits for pending initialization without reading it", () => {
  47. let reads = 0
  48. expect(
  49. initializationReady(
  50. Object.assign(
  51. () => {
  52. reads++
  53. return undefined
  54. },
  55. { error: undefined, loading: true },
  56. ),
  57. ),
  58. ).toBe(false)
  59. expect(reads).toBe(0)
  60. })
  61. })