diff.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Option } from "effect"
  2. import { Headers, HttpBody, HttpClientRequest, UrlParams } from "effect/unstable/http"
  3. import { decodeJson } from "./matching"
  4. import { REDACTED, redactUrl, secretFindings } from "./redaction"
  5. import { httpInteractions, type Cassette, type RequestSnapshot } from "./schema"
  6. const safeText = (value: unknown) => {
  7. if (value === undefined) return "undefined"
  8. if (secretFindings(value).length > 0) return JSON.stringify(REDACTED)
  9. const text = typeof value === "string" ? JSON.stringify(value) : JSON.stringify(value)
  10. if (!text) return String(value)
  11. return text.length > 300 ? `${text.slice(0, 300)}...` : text
  12. }
  13. const jsonBody = (body: string) => Option.getOrUndefined(decodeJson(body))
  14. const valueDiffs = (expected: unknown, received: unknown, base = "$", limit = 8): ReadonlyArray<string> => {
  15. if (Object.is(expected, received)) return []
  16. if (
  17. expected &&
  18. received &&
  19. typeof expected === "object" &&
  20. typeof received === "object" &&
  21. !Array.isArray(expected) &&
  22. !Array.isArray(received)
  23. ) {
  24. return [...new Set([...Object.keys(expected), ...Object.keys(received)])]
  25. .toSorted()
  26. .flatMap((key) =>
  27. valueDiffs(
  28. (expected as Record<string, unknown>)[key],
  29. (received as Record<string, unknown>)[key],
  30. `${base}.${key}`,
  31. limit,
  32. ),
  33. )
  34. .slice(0, limit)
  35. }
  36. if (Array.isArray(expected) && Array.isArray(received)) {
  37. return Array.from({ length: Math.max(expected.length, received.length) }, (_, index) => index)
  38. .flatMap((index) => valueDiffs(expected[index], received[index], `${base}[${index}]`, limit))
  39. .slice(0, limit)
  40. }
  41. return [`${base} expected ${safeText(expected)}, received ${safeText(received)}`]
  42. }
  43. const headerDiffs = (expected: Record<string, string>, received: Record<string, string>) =>
  44. [...new Set([...Object.keys(expected), ...Object.keys(received)])].toSorted().flatMap((key) => {
  45. if (expected[key] === received[key]) return []
  46. if (expected[key] === undefined) return [` ${key} unexpected ${safeText(received[key])}`]
  47. if (received[key] === undefined) return [` ${key} missing expected ${safeText(expected[key])}`]
  48. return [` ${key} expected ${safeText(expected[key])}, received ${safeText(received[key])}`]
  49. })
  50. export const requestDiff = (expected: RequestSnapshot, received: RequestSnapshot) => {
  51. const lines = []
  52. if (expected.method !== received.method) {
  53. lines.push("method:", ` expected ${expected.method}, received ${received.method}`)
  54. }
  55. if (expected.url !== received.url) {
  56. lines.push("url:", ` expected ${expected.url}`, ` received ${received.url}`)
  57. }
  58. const headers = headerDiffs(expected.headers, received.headers)
  59. if (headers.length > 0) lines.push("headers:", ...headers.slice(0, 8))
  60. const expectedBody = jsonBody(expected.body)
  61. const receivedBody = jsonBody(received.body)
  62. const body =
  63. expectedBody !== undefined && receivedBody !== undefined
  64. ? valueDiffs(expectedBody, receivedBody).map((line) => ` ${line}`)
  65. : expected.body === received.body
  66. ? []
  67. : [` expected ${safeText(expected.body)}, received ${safeText(received.body)}`]
  68. if (body.length > 0) lines.push("body:", ...body)
  69. return lines
  70. }
  71. export const mismatchDetail = (cassette: Cassette, incoming: RequestSnapshot) => {
  72. const interactions = httpInteractions(cassette)
  73. if (interactions.length === 0) return "cassette has no recorded HTTP interactions"
  74. const ranked = interactions
  75. .map((interaction, index) => ({ index, lines: requestDiff(interaction.request, incoming) }))
  76. .toSorted((a, b) => a.lines.length - b.lines.length || a.index - b.index)
  77. const best = ranked[0]
  78. return ["no recorded interaction matched", `closest interaction: #${best.index + 1}`, ...best.lines].join("\n")
  79. }
  80. export const redactedErrorRequest = (request: HttpClientRequest.HttpClientRequest) =>
  81. HttpClientRequest.makeWith(
  82. request.method,
  83. redactUrl(request.url),
  84. UrlParams.empty,
  85. Option.none(),
  86. Headers.empty,
  87. HttpBody.empty,
  88. )