import { beforeEach, describe, expect, it, vi } from 'vitest' import { createTool as createGetActiveIncidentsTool } from './get-active-incidents.server' import type { ToolContext } from './types' // Mock IS_PLATFORM vi.mock('common', () => ({ IS_PLATFORM: true, })) const executeOptions = { toolCallId: 'test', messages: [], context: {} } function statusPagePayload(overrides: Record = {}) { return { page_title: 'Supabase', page_url: 'https://status.supabase.com', ongoing_incidents: [], in_progress_maintenances: [], scheduled_maintenances: [], ...overrides, } } const createIncidentTools = (ctx: ToolContext) => ({ get_active_incidents: createGetActiveIncidentsTool(ctx), }) describe('ai/tools/get-active-incidents', () => { let mockFetch: ReturnType let mockAbortSignal: AbortSignal beforeEach(() => { mockFetch = vi.fn() global.fetch = mockFetch as typeof fetch // Mock AbortSignal.timeout mockAbortSignal = new AbortController().signal if (!AbortSignal.timeout) { AbortSignal.timeout = vi.fn(() => mockAbortSignal) as any } }) describe('incident tools', () => { describe('execute function', () => { it('should return empty incidents when not on platform', async () => { const common = await import('common') vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(false) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect(result).toEqual({ incidents: [], message: 'Incident checking is only available on Supabase platform.', }) expect(mockFetch).not.toHaveBeenCalled() }) it('throws on fetch errors', async () => { const common = await import('common') vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true) mockFetch.mockRejectedValue(new Error('Network error')) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) await expect( (tools.get_active_incidents.execute as any)({}, executeOptions) ).rejects.toThrow('Network error') }) it('should use timeout signal', async () => { const common = await import('common') vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true) mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload(), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) if (!tools.get_active_incidents.execute) throw new Error('execute is undefined') await tools.get_active_incidents.execute( {}, { toolCallId: 'test', messages: [], context: {} } ) const callArgs = mockFetch.mock.calls[0] expect(callArgs[1].signal).toBeInstanceOf(AbortSignal) }) it('cancels the request when the Assistant request is aborted', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload(), }) const abortController = new AbortController() const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) if (!tools.get_active_incidents.execute) throw new Error('execute is undefined') await tools.get_active_incidents.execute( {}, { ...executeOptions, abortSignal: abortController.signal } ) const { signal } = mockFetch.mock.calls[0][1] expect(signal.aborted).toBe(false) abortController.abort() expect(signal.aborted).toBe(true) }) }) }) describe('status page response', () => { beforeEach(async () => { const common = await import('common') vi.spyOn(common, 'IS_PLATFORM', 'get').mockReturnValue(true) }) const baseIncident = { id: 'incident-1', name: 'Elevated error rates', url: 'https://status.supabase.com/incidents/incident-1', last_update_at: '2026-01-01T00:00:00Z', last_update_message: 'We are investigating.', affected_components: [], status: 'investigating', current_worst_impact: 'partial_outage', } const baseMaintenance = { id: 'maintenance-1', name: 'Scheduled database maintenance', url: 'https://status.supabase.com/maintenances/maintenance-1', last_update_at: '2026-01-01T00:00:00Z', last_update_message: null, affected_components: [], status: 'maintenance_in_progress', started_at: '2026-01-01T00:00:00Z', } it('fetches from the status page endpoint', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload(), }) const tools = createIncidentTools({ baseUrl: 'https://example.com/dashboard', aiOptInLevel: 'schema', }) if (!tools.get_active_incidents.execute) throw new Error('execute is undefined') await tools.get_active_incidents.execute({}, executeOptions) expect(mockFetch).toHaveBeenCalledWith('https://example.com/dashboard/api/status-page', { signal: expect.any(AbortSignal), }) }) it('drops incidents and maintenances where visible is false', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload({ ongoing_incidents: [ { ...baseIncident, visible: false, show_banner: false }, { ...baseIncident, id: 'incident-2', visible: true, show_banner: true }, ], in_progress_maintenances: [{ ...baseMaintenance, visible: false, show_banner: false }], }), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect((result as any).incidents).toHaveLength(1) expect((result as any).incidents[0]).toMatchObject({ kind: 'incident', name: 'Elevated error rates', }) }) it('includes items with show_banner set to false', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload({ ongoing_incidents: [{ ...baseIncident, visible: true, show_banner: false }], }), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect((result as any).incidents).toHaveLength(1) }) it('combines ongoing incidents and in-progress maintenances, excluding scheduled maintenances', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload({ ongoing_incidents: [{ ...baseIncident, visible: true, show_banner: true }], in_progress_maintenances: [{ ...baseMaintenance, visible: true, show_banner: true }], scheduled_maintenances: [ { id: 'scheduled-1', name: 'Upcoming maintenance', url: 'https://status.supabase.com/maintenances/scheduled-1', last_update_at: '2026-01-01T00:00:00Z', last_update_message: null, affected_components: [], status: 'maintenance_scheduled', starts_at: '2026-02-01T00:00:00Z', ends_at: '2026-02-01T01:00:00Z', visible: true, show_banner: true, banner_lead_days: 1, }, ], }), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect((result as any).incidents).toHaveLength(2) expect((result as any).incidents.map((incident: any) => incident.kind).sort()).toEqual([ 'incident', 'maintenance', ]) expect((result as any).message).toContain('status.supabase.com') }) it('describes maintenance-only results as maintenance, not as an incident', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload({ in_progress_maintenances: [{ ...baseMaintenance, visible: true, show_banner: true }], }), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect((result as any).incidents).toHaveLength(1) expect((result as any).message).toContain('1 in-progress maintenance') expect((result as any).message).not.toContain('active incident') }) it('returns the no-active-incidents message when nothing is visible', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => statusPagePayload(), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) const result = await (tools.get_active_incidents.execute as any)({}, executeOptions) expect(result).toEqual({ incidents: [], message: expect.stringContaining('No active incidents'), }) }) it('throws when the response fails to parse', async () => { mockFetch.mockResolvedValue({ ok: true, json: async () => ({ this_is: 'not a valid status page response' }), }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow( 'Failed to parse status page response' ) }) it('throws on a non-ok response with the status code', async () => { mockFetch.mockResolvedValue({ ok: false, status: 500, }) const tools = createIncidentTools({ baseUrl: 'https://supabase.com/dashboard', aiOptInLevel: 'schema', }) await expect((tools.get_active_incidents.execute as any)({}, executeOptions)).rejects.toThrow( 'Failed to fetch status page: 500' ) }) }) })