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.
83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { BackgroundRefresher } from '~/common/utils/background-refresher'
|
|
|
|
const realNow = Date.now
|
|
|
|
describe('getNextRetryMs', () => {
|
|
jest.useFakeTimers()
|
|
jest.setTimeout(1000)
|
|
let refreshFunction = jest.fn()
|
|
let refresher = new BackgroundRefresher(refreshFunction)
|
|
|
|
beforeEach(() => {
|
|
refreshFunction = jest.fn()
|
|
refresher = new BackgroundRefresher(refreshFunction, 100)
|
|
})
|
|
|
|
beforeAll(() => {
|
|
global.Date.now = jest.fn(() => new Date('2019-04-07T10:20:30Z').getTime())
|
|
})
|
|
|
|
afterAll(() => {
|
|
global.Date.now = realNow
|
|
})
|
|
|
|
it('simple gets', async () => {
|
|
refreshFunction.mockResolvedValue('foo')
|
|
await expect(refresher.get()).resolves.toEqual('foo')
|
|
refreshFunction.mockResolvedValue('foo2')
|
|
await expect(refresher.get()).resolves.toEqual('foo')
|
|
await expect(refresher.refresh()).resolves.toEqual('foo2')
|
|
await expect(refresher.get()).resolves.toEqual('foo2')
|
|
})
|
|
|
|
it('only one call per refresh', async () => {
|
|
refreshFunction.mockImplementation(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
return 'foo'
|
|
})
|
|
|
|
const promises: Promise<any>[] = []
|
|
|
|
expect(refreshFunction).toHaveBeenCalledTimes(0)
|
|
promises.push(refresher.get())
|
|
expect(refreshFunction).toHaveBeenCalledTimes(1)
|
|
promises.push(refresher.get())
|
|
promises.push(refresher.get())
|
|
expect(refreshFunction).toHaveBeenCalledTimes(1)
|
|
jest.runOnlyPendingTimers()
|
|
|
|
expect(await Promise.all(promises)).toEqual(['foo', 'foo', 'foo'])
|
|
})
|
|
|
|
it('refreshes in the background', async () => {
|
|
let count = 1
|
|
let timeAdavance = 0
|
|
global.Date.now = jest.fn(() => realNow() + timeAdavance)
|
|
refresher = new BackgroundRefresher(refreshFunction, 10000)
|
|
|
|
refreshFunction.mockImplementation(async () => {
|
|
await new Promise((r) => setTimeout(r, 1000))
|
|
return 'foo' + count++
|
|
})
|
|
|
|
// First time we need to trigger the timer before awaiting as it waits for the value
|
|
let response = refresher.get()
|
|
jest.runOnlyPendingTimers()
|
|
await expect(response).resolves.toEqual('foo1')
|
|
await expect(refresher.get()).resolves.toEqual('foo1')
|
|
expect(refreshFunction).toHaveBeenCalledTimes(1)
|
|
// Advance time forward by more than the refresh interval
|
|
timeAdavance = 10000 + 1000
|
|
// This will trigger the background refresh
|
|
response = refresher.get()
|
|
// which we resolve the timeout for
|
|
jest.runOnlyPendingTimers()
|
|
// the original call gets the old value as it doesn't wait
|
|
await expect(response).resolves.toEqual('foo1')
|
|
expect(refreshFunction).toHaveBeenCalledTimes(2)
|
|
// the next call gets the new value
|
|
await expect(refresher.get()).resolves.toEqual('foo2')
|
|
expect(refreshFunction).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|