share-next.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { test, expect, mock } from "bun:test"
  2. import { ShareNext } from "../../src/share/share-next"
  3. import { AccessToken, Account, AccountID, OrgID } from "../../src/account"
  4. import { Config } from "../../src/config/config"
  5. test("ShareNext.request uses legacy share API without active org account", async () => {
  6. const originalActive = Account.active
  7. const originalConfigGet = Config.get
  8. Account.active = mock(async () => undefined)
  9. Config.get = mock(async () => ({ enterprise: { url: "https://legacy-share.example.com" } }))
  10. try {
  11. const req = await ShareNext.request()
  12. expect(req.api.create).toBe("/api/share")
  13. expect(req.api.sync("shr_123")).toBe("/api/share/shr_123/sync")
  14. expect(req.api.remove("shr_123")).toBe("/api/share/shr_123")
  15. expect(req.api.data("shr_123")).toBe("/api/share/shr_123/data")
  16. expect(req.baseUrl).toBe("https://legacy-share.example.com")
  17. expect(req.headers).toEqual({})
  18. } finally {
  19. Account.active = originalActive
  20. Config.get = originalConfigGet
  21. }
  22. })
  23. test("ShareNext.request uses org share API with auth headers when account is active", async () => {
  24. const originalActive = Account.active
  25. const originalToken = Account.token
  26. Account.active = mock(async () => ({
  27. id: AccountID.make("account-1"),
  28. email: "user@example.com",
  29. url: "https://control.example.com",
  30. active_org_id: OrgID.make("org-1"),
  31. }))
  32. Account.token = mock(async () => AccessToken.make("st_test_token"))
  33. try {
  34. const req = await ShareNext.request()
  35. expect(req.api.create).toBe("/api/shares")
  36. expect(req.api.sync("shr_123")).toBe("/api/shares/shr_123/sync")
  37. expect(req.api.remove("shr_123")).toBe("/api/shares/shr_123")
  38. expect(req.api.data("shr_123")).toBe("/api/shares/shr_123/data")
  39. expect(req.baseUrl).toBe("https://control.example.com")
  40. expect(req.headers).toEqual({
  41. authorization: "Bearer st_test_token",
  42. "x-org-id": "org-1",
  43. })
  44. } finally {
  45. Account.active = originalActive
  46. Account.token = originalToken
  47. }
  48. })
  49. test("ShareNext.request fails when org account has no token", async () => {
  50. const originalActive = Account.active
  51. const originalToken = Account.token
  52. Account.active = mock(async () => ({
  53. id: AccountID.make("account-1"),
  54. email: "user@example.com",
  55. url: "https://control.example.com",
  56. active_org_id: OrgID.make("org-1"),
  57. }))
  58. Account.token = mock(async () => undefined)
  59. try {
  60. await expect(ShareNext.request()).rejects.toThrow("No active account token available for sharing")
  61. } finally {
  62. Account.active = originalActive
  63. Account.token = originalToken
  64. }
  65. })