auth.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import { Auth } from "../../src/auth"
  4. import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
  5. import { provideTmpdirInstance } from "../fixture/fixture"
  6. import { testEffect } from "../lib/effect"
  7. const node = CrossSpawnSpawner.defaultLayer
  8. const it = testEffect(Layer.mergeAll(Auth.defaultLayer, node))
  9. describe("Auth", () => {
  10. it.live("set normalizes trailing slashes in keys", () =>
  11. provideTmpdirInstance(() =>
  12. Effect.gen(function* () {
  13. const auth = yield* Auth.Service
  14. yield* auth.set("https://example.com/", {
  15. type: "wellknown",
  16. key: "TOKEN",
  17. token: "abc",
  18. })
  19. const data = yield* auth.all()
  20. expect(data["https://example.com"]).toBeDefined()
  21. expect(data["https://example.com/"]).toBeUndefined()
  22. }),
  23. ),
  24. )
  25. it.live("set cleans up pre-existing trailing-slash entry", () =>
  26. provideTmpdirInstance(() =>
  27. Effect.gen(function* () {
  28. const auth = yield* Auth.Service
  29. yield* auth.set("https://example.com/", {
  30. type: "wellknown",
  31. key: "TOKEN",
  32. token: "old",
  33. })
  34. yield* auth.set("https://example.com", {
  35. type: "wellknown",
  36. key: "TOKEN",
  37. token: "new",
  38. })
  39. const data = yield* auth.all()
  40. const keys = Object.keys(data).filter((key) => key.includes("example.com"))
  41. expect(keys).toEqual(["https://example.com"])
  42. const entry = data["https://example.com"]!
  43. expect(entry.type).toBe("wellknown")
  44. if (entry.type === "wellknown") expect(entry.token).toBe("new")
  45. }),
  46. ),
  47. )
  48. it.live("remove deletes both trailing-slash and normalized keys", () =>
  49. provideTmpdirInstance(() =>
  50. Effect.gen(function* () {
  51. const auth = yield* Auth.Service
  52. yield* auth.set("https://example.com", {
  53. type: "wellknown",
  54. key: "TOKEN",
  55. token: "abc",
  56. })
  57. yield* auth.remove("https://example.com/")
  58. const data = yield* auth.all()
  59. expect(data["https://example.com"]).toBeUndefined()
  60. expect(data["https://example.com/"]).toBeUndefined()
  61. }),
  62. ),
  63. )
  64. it.live("set and remove are no-ops on keys without trailing slashes", () =>
  65. provideTmpdirInstance(() =>
  66. Effect.gen(function* () {
  67. const auth = yield* Auth.Service
  68. yield* auth.set("anthropic", {
  69. type: "api",
  70. key: "sk-test",
  71. })
  72. const data = yield* auth.all()
  73. expect(data["anthropic"]).toBeDefined()
  74. yield* auth.remove("anthropic")
  75. const after = yield* auth.all()
  76. expect(after["anthropic"]).toBeUndefined()
  77. }),
  78. ),
  79. )
  80. })