Skip to content

Commit

Permalink
🔨 add tests for base64/hex conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Aug 8, 2024
1 parent ff63d5d commit f0b4e7e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/@ourworldindata/utils/src/Util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ import {
traverseEnrichedBlock,
cartesian,
formatInlineList,
base64ToBytes,
bytesToBase64,
hexToBytes,
bytesToHex,
} from "./Util.js"
import {
BlockImageSize,
OwidEnrichedGdocBlock,
SortOrder,
} from "@ourworldindata/types"
import { webcrypto as crypto } from "node:crypto"

describe(findClosestTime, () => {
describe("without tolerance", () => {
Expand Down Expand Up @@ -795,3 +800,24 @@ describe(formatInlineList, () => {
)
})
})

function generateRandomBytes(length: number): Uint8Array {
const bytes = new Uint8Array(length)
crypto.getRandomValues(bytes)
return bytes
}

describe("hex/base64 conversion is reversible", () => {
const originalBytes = generateRandomBytes(33)
const base64String = bytesToBase64(originalBytes)
const roundTrippedBytes = base64ToBytes(base64String)
it("is the same after converting to base64 and back", () => {
expect(originalBytes).toEqual(roundTrippedBytes)
})

const hexString = bytesToHex(originalBytes)
const roundTrippedBytesHex = hexToBytes(hexString)
it("is the same after converting to hex and back", () => {
expect(originalBytes).toEqual(roundTrippedBytesHex)
})
})

0 comments on commit f0b4e7e

Please sign in to comment.