key.test.js 932 Bytes
import { describe, it, expect } from 'vitest'
import { bucketKey, KEY_NAMESPACE } from '../src/key.js'

describe('key', () => {
  it('带命名空间前缀(防与其它 redis key 撞)', () => {
    const k = bucketKey({ endpoint: 'llm', by: 'ip' }, '1.2.3.4')
    expect(k.startsWith(`${KEY_NAMESPACE}\x1f`)).toBe(true)
  })

  it('不同 endpoint / subject / by 不串桶', () => {
    const a = bucketKey({ endpoint: 'llm', by: 'ip' }, '1.2.3.4')
    const b = bucketKey({ endpoint: 'tts', by: 'ip' }, '1.2.3.4')
    const c = bucketKey({ endpoint: 'llm', by: 'ip' }, '5.6.7.8')
    const d = bucketKey({ endpoint: 'llm', by: 'anon' }, '1.2.3.4')
    expect(new Set([a, b, c, d]).size).toBe(4)
  })

  it('subject 含分隔符样字符不与结构分隔混淆', () => {
    const a = bucketKey({ endpoint: 'llm', by: 'ip' }, 'a:b')
    const b = bucketKey({ endpoint: 'llm', by: 'ip' }, 'a')
    expect(a).not.toBe(b)
  })
})