mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
A key now opens with what it is allowed to touch. `pk-` is publishable and safe in a browser; `sk-` is secret and never leaves a server. The five marks it replaces all began `ph` -- the upstream's initials -- and they were the last of its brand a customer read back to us, in every SDK snippet. No word rule could reach them, because `ph` is not a word. The marks were also not telling the truth. The constant said `phc_` while every live project token said `hi_`, so the middleware that routes on the constant matched no real token at all. Two spellings existed and one was fiction. Minting now happens in exactly one function and reading in exactly one, so a second spelling has nowhere to live. `phx_` (personal) and `phs_` (project secret) both become `sk-`. The mark now says only that a value is secret; which store holds it is settled by asking. That was already how personal keys worked -- keys minted before the marks carry none, so the store always decided -- and both secret resolvers already returned None on a miss and fell through, so one shared extractor replaces the two that could drift apart. OAuth's access and refresh tokens stay distinguishable as `at-` and `rt-`, because the parser tells them apart before either is looked up, and the domain-verification token becomes `vt-` rather than keeping a sixth mint of its own. `bin/debrand --check` now refuses the marks as a shape, so a seventh cannot be coined. Two rules that were about to rename things that are not ours to spell are bounded: `Lemon` no longer eats `LemonSqueezy`, and the `hog_flow` rules no longer reach a Postgres table, a Datastore table or a route. Keys are stored hashed, so a mark cannot be rewritten in place: the three live project tokens have to be reminted.
91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { InsightsApiError, InsightsPermissionError, InsightsRateLimitError, InsightsValidationError } from '@/lib/errors'
|
|
import { invokeMcpTool } from '@/tools/insightsAiTools/invokeTool'
|
|
import type { Context } from '@/tools/types'
|
|
|
|
function makeContext(): Context {
|
|
return {
|
|
api: {
|
|
baseUrl: 'https://us.hanzo.ai',
|
|
config: { apiToken: 'sk-test' },
|
|
},
|
|
stateManager: {
|
|
getProjectId: vi.fn().mockResolvedValue(2),
|
|
},
|
|
} as unknown as Context
|
|
}
|
|
|
|
describe('invokeMcpTool', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
const stubFetch = (response: Response): void => {
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response))
|
|
}
|
|
|
|
it('returns the tool result on success', async () => {
|
|
stubFetch(new Response(JSON.stringify({ success: true, content: 'rows' }), { status: 200 }))
|
|
|
|
const result = await invokeMcpTool(makeContext(), 'execute_sql', { query: 'SELECT 1' })
|
|
|
|
expect(result).toEqual({ success: true, content: 'rows' })
|
|
})
|
|
|
|
it('throws InsightsRateLimitError on a 429 so it is not bucketed as an internal error', async () => {
|
|
stubFetch(
|
|
new Response(JSON.stringify({ detail: 'Request was throttled.' }), {
|
|
status: 429,
|
|
headers: { 'Retry-After': '7' },
|
|
})
|
|
)
|
|
|
|
const error = await invokeMcpTool(makeContext(), 'execute_sql', { query: 'SELECT 1' }).catch((e) => e)
|
|
|
|
expect(error).toBeInstanceOf(InsightsRateLimitError)
|
|
expect(error).toBeInstanceOf(InsightsApiError)
|
|
expect((error as InsightsRateLimitError).status).toBe(429)
|
|
expect((error as InsightsRateLimitError).retryAfterSeconds).toBe(7)
|
|
})
|
|
|
|
it('throws InsightsApiError carrying the status on a 5xx', async () => {
|
|
stubFetch(new Response('upstream exploded', { status: 503, statusText: 'Service Unavailable' }))
|
|
|
|
const error = await invokeMcpTool(makeContext(), 'execute_sql', { query: 'SELECT 1' }).catch((e) => e)
|
|
|
|
expect(error).toBeInstanceOf(InsightsApiError)
|
|
expect((error as InsightsApiError).status).toBe(503)
|
|
})
|
|
|
|
it('throws InsightsPermissionError on a 403 permission_denied', async () => {
|
|
stubFetch(
|
|
new Response(JSON.stringify({ code: 'permission_denied', detail: "required scope 'query:read'" }), {
|
|
status: 403,
|
|
})
|
|
)
|
|
|
|
const error = await invokeMcpTool(makeContext(), 'execute_sql', { query: 'SELECT 1' }).catch((e) => e)
|
|
|
|
expect(error).toBeInstanceOf(InsightsPermissionError)
|
|
expect((error as InsightsPermissionError).missingScope).toBe('query:read')
|
|
})
|
|
|
|
it('throws InsightsValidationError on a validation_error body', async () => {
|
|
stubFetch(
|
|
new Response(JSON.stringify({ type: 'validation_error', detail: 'bad query', attr: 'query' }), {
|
|
status: 400,
|
|
})
|
|
)
|
|
|
|
const error = await invokeMcpTool(makeContext(), 'execute_sql', { query: 'SELECT 1' }).catch((e) => e)
|
|
|
|
expect(error).toBeInstanceOf(InsightsValidationError)
|
|
expect((error as InsightsValidationError).attr).toBe('query')
|
|
})
|
|
})
|