auth.test.ts 2.4 KB

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