Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3
4import { contentHash, getJson, putJson, stableStringify } from './kv.ts'
5
6describe('stableStringify', () => {
7 it('is independent of object key order, including nested objects', () => {
8 expect(stableStringify({ b: 1, a: { d: 2, c: [{ f: 3, e: 4 }] } })).toBe(
9 stableStringify({ a: { c: [{ e: 4, f: 3 }], d: 2 }, b: 1 }),
10 )
11 })
12
13 it('preserves array order', () => {
14 expect(stableStringify([2, 1])).toBe('[2,1]')
15 })
16})
17
18describe('contentHash', () => {
19 it('produces a stable sha-256 hex digest for equivalent values', async () => {
20 const a = await contentHash({ x: 1, y: 2 })
21 const b = await contentHash({ y: 2, x: 1 })
22 expect(a).toBe(b)
23 expect(a).toMatch(/^[0-9a-f]{64}$/)
24 })
25
26 it('differs for different values', async () => {
27 expect(await contentHash({ x: 1 })).not.toBe(await contentHash({ x: 2 }))
28 })
29})
30
31describe('kv json helpers (real KV binding)', () => {
32 it('round-trips a JSON value', async () => {
33 const value = { schema: { type: 'object' }, b: 2, a: [1, 2, 3] }
34 await putJson(env.SCHEMA_CACHE, 'test:round-trip', value)
35 expect(await getJson(env.SCHEMA_CACHE, 'test:round-trip')).toEqual(value)
36 })
37
38 it('returns null for a missing key', async () => {
39 expect(await getJson(env.SCHEMA_CACHE, 'test:missing')).toBeNull()
40 })
41})