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.
119 lines
4.3 KiB
TypeScript
119 lines
4.3 KiB
TypeScript
// eslint-disable-next-line no-restricted-imports
|
|
import fetch from 'node-fetch'
|
|
|
|
import { installPostgresTypeParsers } from './src/common/utils/db/postgres'
|
|
import { logger, shutdownLogger } from './src/common/utils/logger'
|
|
|
|
const { readFileSync } = require('fs')
|
|
const { DateTime } = require('luxon')
|
|
const { join } = require('path')
|
|
|
|
// Sets up the same type parsers as used everywhere else in the codebase
|
|
installPostgresTypeParsers()
|
|
|
|
// Mock @pyroscope/nodejs to avoid ESM compatibility issues with p-limit
|
|
jest.mock('@pyroscope/nodejs', () => ({
|
|
init: jest.fn(),
|
|
start: jest.fn(),
|
|
stop: jest.fn(),
|
|
}))
|
|
|
|
// Setup spies on the logger for all tests to use
|
|
|
|
jest.mock('node-fetch', () => ({
|
|
__esModule: true,
|
|
...jest.requireActual('node-fetch'), // Only mock fetch(), leave Request, Response, FetchError, etc. alone
|
|
default: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('undici', () => ({
|
|
__esModule: true,
|
|
...jest.requireActual('undici'), // Only mock fetch(), leave Request, Response, FetchError, etc. alone
|
|
fetch: jest.spyOn(jest.requireActual('undici'), 'fetch'),
|
|
}))
|
|
|
|
beforeEach(() => {
|
|
jest.spyOn(logger, 'info')
|
|
jest.spyOn(logger, 'warn')
|
|
jest.spyOn(logger, 'debug')
|
|
jest.spyOn(logger, 'error')
|
|
|
|
jest.mocked(logger.info).mockClear()
|
|
jest.mocked(logger.warn).mockClear()
|
|
jest.mocked(logger.debug).mockClear()
|
|
jest.mocked(logger.error).mockClear()
|
|
|
|
const responsesToUrls: Record<string, any> = {
|
|
'https://google.com/results.json?query=fetched': { count: 2, query: 'bla', results: [true, true] },
|
|
'https://mmdbcdn.insights.net/': readFileSync(join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')),
|
|
'https://app.hanzo.ai/api/event?token=THIS+IS+NOT+A+TOKEN+FOR+TEAM+2': { hello: 'world' },
|
|
'https://onevent.com/': { success: true },
|
|
'https://www.example.com': { example: 'data' },
|
|
}
|
|
|
|
const headersToUrls: Record<string, Map<string, string>> = {
|
|
'https://mmdbcdn.insights.net/': new Map([
|
|
['content-type', 'vnd.maxmind.maxmind-db'],
|
|
['content-disposition', `attachment; filename="GeoLite2-City-${DateTime.local().toISODate()}.mmdb"`],
|
|
]),
|
|
}
|
|
|
|
// Create a proper Response-like object factory
|
|
const createMockResponse = (url: string, options: any = {}): any => {
|
|
const responseBody = responsesToUrls[url] || { fetch: 'mock' }
|
|
const responseHeaders = headersToUrls[url] || new Map()
|
|
const responseText =
|
|
typeof responseBody === 'object' && !Buffer.isBuffer(responseBody)
|
|
? JSON.stringify(responseBody)
|
|
: String(responseBody)
|
|
|
|
// Create a proper Response-like object that matches the interface expected by recordedFetch
|
|
return {
|
|
// Properties
|
|
status: options.method === 'PUT' ? 201 : 200,
|
|
statusText: 'OK',
|
|
ok: true,
|
|
headers: {
|
|
get: (name: string) => {
|
|
if (responseHeaders instanceof Map) {
|
|
return responseHeaders.get(name) || null
|
|
}
|
|
return null
|
|
},
|
|
forEach: (callback: (value: string, key: string) => void) => {
|
|
if (responseHeaders instanceof Map) {
|
|
responseHeaders.forEach((value, key) => callback(value, key))
|
|
}
|
|
},
|
|
},
|
|
|
|
// Methods
|
|
buffer: () => Promise.resolve(Buffer.isBuffer(responseBody) ? responseBody : Buffer.from(responseText)),
|
|
json: () => Promise.resolve(responseBody),
|
|
text: () => Promise.resolve(responseText),
|
|
|
|
// Clone method that returns a similar object with the same interface
|
|
clone: function () {
|
|
return createMockResponse(url, options)
|
|
},
|
|
}
|
|
}
|
|
|
|
jest.mocked(fetch as any).mockImplementation((url: string, options: any = {}) => {
|
|
return Promise.resolve(createMockResponse(url, options))
|
|
})
|
|
})
|
|
|
|
beforeAll(() => {
|
|
// We use procese.exit in a few places, which end up terminating tests
|
|
// if we don't mock it.
|
|
jest.spyOn(process, 'exit').mockImplementation((number) => {
|
|
throw new Error('process.exit: ' + number)
|
|
})
|
|
})
|
|
|
|
afterAll(async () => {
|
|
// Shutdown logger to prevent Jest from hanging on open handles
|
|
await shutdownLogger()
|
|
})
|