mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
151 lines
4.8 KiB
TypeScript
151 lines
4.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { RedisLike } from '@/hono/cache/RedisCache'
|
|
import { SharedBlobCache, type SharedBlobCacheOptions } from '@/hono/cache/SharedBlobCache'
|
|
|
|
import { makeRedisRateLimitStubs } from './helpers/redis-rate-limit-stubs'
|
|
|
|
class TestSharedBlobCache extends SharedBlobCache {
|
|
constructor(redis: RedisLike, namespace: string, opts?: SharedBlobCacheOptions) {
|
|
super(redis, namespace, opts)
|
|
}
|
|
|
|
read(): Promise<{ bytes: Uint8Array; fresh: boolean } | null> {
|
|
return this.readCache()
|
|
}
|
|
|
|
write(bytes: Uint8Array): Promise<void> {
|
|
return this.writeCache(bytes)
|
|
}
|
|
|
|
acquire(token: string): Promise<boolean> {
|
|
return this.acquireLock(token)
|
|
}
|
|
|
|
release(token: string): Promise<void> {
|
|
return this.releaseLock(token)
|
|
}
|
|
|
|
wait(): Promise<Uint8Array | null> {
|
|
return this.waitForCache()
|
|
}
|
|
}
|
|
|
|
interface MockRedis extends RedisLike {
|
|
_store: Map<string, string>
|
|
_setCalls: Array<{ key: string; value: string; args: (string | number)[] }>
|
|
}
|
|
|
|
function createMockRedis(): MockRedis {
|
|
const store = new Map<string, string>()
|
|
const setCalls: Array<{ key: string; value: string; args: (string | number)[] }> = []
|
|
return {
|
|
get: vi.fn(async (key: string) => store.get(key) ?? null),
|
|
set: vi.fn(async (key: string, value: string, ...args: (string | number)[]) => {
|
|
setCalls.push({ key, value, args })
|
|
const isNx = args.includes('NX')
|
|
if (isNx && store.has(key)) {
|
|
return null
|
|
}
|
|
store.set(key, value)
|
|
return 'OK'
|
|
}),
|
|
del: vi.fn(async (...keys: string[]) => {
|
|
let count = 0
|
|
for (const k of keys) {
|
|
if (store.delete(k)) {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}),
|
|
scan: vi.fn(async () => ['0', []] as [string, string[]]),
|
|
...makeRedisRateLimitStubs(),
|
|
_store: store,
|
|
_setCalls: setCalls,
|
|
}
|
|
}
|
|
|
|
const NAMESPACE = 'test-blob'
|
|
const BYTES_KEY = `mcp:shared-blob:${NAMESPACE}:bytes`
|
|
const FRESH_KEY = `mcp:shared-blob:${NAMESPACE}:fresh`
|
|
const LOCK_KEY = `mcp:shared-blob:${NAMESPACE}:lock`
|
|
|
|
describe('SharedBlobCache', () => {
|
|
let redis: MockRedis
|
|
|
|
beforeEach(() => {
|
|
redis = createMockRedis()
|
|
})
|
|
|
|
it('writes bytes + freshness and reads them back', async () => {
|
|
const bytes = new Uint8Array([1, 2, 3, 4, 5])
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE)
|
|
|
|
await cache.write(bytes)
|
|
const cached = await cache.read()
|
|
|
|
expect(cached).toEqual({ bytes, fresh: true })
|
|
expect(redis._store.has(BYTES_KEY)).toBe(true)
|
|
expect(redis._store.has(FRESH_KEY)).toBe(true)
|
|
})
|
|
|
|
it('marks cached bytes stale after the freshness window', async () => {
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE, { freshSeconds: 0 })
|
|
await cache.write(new Uint8Array([9, 8, 7]))
|
|
|
|
const cached = await cache.read()
|
|
|
|
expect(cached?.bytes).toEqual(new Uint8Array([9, 8, 7]))
|
|
expect(cached?.fresh).toBe(false)
|
|
})
|
|
|
|
it('isolates blobs by namespace', async () => {
|
|
const a = new TestSharedBlobCache(redis, 'alpha')
|
|
const b = new TestSharedBlobCache(redis, 'beta')
|
|
await a.write(new Uint8Array([1]))
|
|
await b.write(new Uint8Array([2]))
|
|
|
|
expect(redis._store.has('mcp:shared-blob:alpha:bytes')).toBe(true)
|
|
expect(redis._store.has('mcp:shared-blob:beta:bytes')).toBe(true)
|
|
|
|
expect((await a.read())?.bytes).toEqual(new Uint8Array([1]))
|
|
expect((await b.read())?.bytes).toEqual(new Uint8Array([2]))
|
|
})
|
|
|
|
it('allows only one lock holder', async () => {
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE)
|
|
|
|
expect(await cache.acquire('first')).toBe(true)
|
|
expect(await cache.acquire('second')).toBe(false)
|
|
|
|
expect(redis._store.get(LOCK_KEY)).toBe('first')
|
|
})
|
|
|
|
it('releases the lock', async () => {
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE)
|
|
|
|
await cache.acquire('token')
|
|
await cache.release('token')
|
|
|
|
expect(redis._store.has(LOCK_KEY)).toBe(false)
|
|
})
|
|
|
|
it('waits for another writer to publish', async () => {
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE, { waitIntervalMs: 5, waitTimeoutMs: 200 })
|
|
const bytes = new Uint8Array([55, 56])
|
|
|
|
const waited = cache.wait()
|
|
await new Promise((r) => setTimeout(r, 20))
|
|
await cache.write(bytes)
|
|
|
|
expect(await waited).toEqual(bytes)
|
|
})
|
|
|
|
it('times out while waiting for another writer', async () => {
|
|
const cache = new TestSharedBlobCache(redis, NAMESPACE, { waitIntervalMs: 10, waitTimeoutMs: 50 })
|
|
|
|
expect(await cache.wait()).toBeNull()
|
|
})
|
|
})
|