Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

4.3 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { z } from 'zod'

import { DashboardsPartialUpdateBody } from '@/generated/dashboards/api'
import { GENERATED_TOOLS } from '@/tools/generated/dashboards'
import { getToolDefinitions } from '@/tools/toolDefinitions'

function getSchemaShape(schema: z.ZodTypeAny): Record<string, z.ZodTypeAny> {
    if ('shape' in schema && schema.shape && typeof schema.shape === 'object') {
        return schema.shape as Record<string, z.ZodTypeAny>
    }
    const inner = (schema._def as { schema?: z.ZodTypeAny }).schema
    if (inner) {
        return getSchemaShape(inner)
    }
    throw new Error(`Expected object schema, got ${schema.constructor.name}`)
}

describe('dashboard-update schema', () => {
    const tool = GENERATED_TOOLS['dashboard-update']!()

    it('includes every OpenAPI PATCH body field from DashboardsPartialUpdateBody', () => {
        const toolShape = getSchemaShape(tool.schema)
        const openapiBodyKeys = Object.keys(DashboardsPartialUpdateBody().shape)

        for (const param of openapiBodyKeys) {
            expect(toolShape[param], `dashboard-update schema missing OpenAPI field: ${param}`).not.toBeUndefined()
        }
    })

    it('accepts optional dashboard PATCH write params', () => {
        const result = tool.schema.safeParse({
            id: 1,
            breakdown_colors: [{ breakdownValue: 'Chrome', breakdownType: 'event', colorToken: 'preset-1' }],
            data_color_theme_id: 2,
            quick_filter_ids: ['00000000-0000-4000-8000-000000000001'],
            use_template: '',
            use_dashboard: null,
            delete_insights: false,
            tiles: [{ id: 1, layouts: { sm: { x: 0, y: 0, w: 6, h: 4 } } }],
        })

        expect(result.success).toBe(true)
    })

    // Widget settings go through dashboard-widgets-batch-update. Every widget type's config schema
    // would otherwise be copied into this tool, which loads without the dashboard-widgets flag.
    it('leaves widget settings out of tiles', () => {
        const tiles = z.toJSONSchema(tool.schema, { io: 'input' }).properties?.tiles as {
            items?: { properties?: Record<string, unknown> }
        }
        const tileProperties = tiles.items?.properties

        expect(tileProperties).toHaveProperty('layouts')
        expect(tileProperties).not.toHaveProperty('widget')
    })

    // The schema used to accept any JSON for this field, and its description read as a color
    // mapping, so agents sent a dictionary of breakdown values to hex colors. The dashboard cannot
    // read that shape.
    it.each([
        ['an object keyed by breakdown value', { series_a: '#ff0000' }],
        ['entries under other key names', [{ breakdown_value: 'good', color: '#36a854' }]],
        ['an entry without a color token', [{ breakdownValue: 'Chrome' }]],
        ['a hex value where a palette slot belongs', [{ breakdownValue: 'Chrome', colorToken: '#3fb950' }]],
        // Also proves the generated schema carries the serializer's token pattern.
        ['palette slot zero', [{ breakdownValue: 'Chrome', colorToken: 'preset-0' }]],
    ])('rejects breakdown_colors as %s', (_name, breakdownColors) => {
        const result = tool.schema.safeParse({ id: 1, breakdown_colors: breakdownColors })

        expect(result.success).toBe(false)
    })
})

describe('dashboard layout tool descriptions', () => {
    it.each([
        ['dashboard-update', /Resize, reposition, or update dashboard tiles/],
        ['dashboard-reorder-tiles', /To repack the whole dashboard, include every tile ID/],
        ['dashboard-transfer-tile', /source dashboard to another dashboard/],
    ])('distinguishes %s from other layout tools', (name, expectedDescription) => {
        expect(getToolDefinitions()[name]?.description).toMatch(expectedDescription)
    })
})

describe('dashboard-transfer-tile schema', () => {
    const tool = GENERATED_TOOLS['dashboard-transfer-tile']!()

    it.each([
        ['tile', { id: 1, to_dashboard: 2 }],
        ['to_dashboard', { id: 1, tile: { id: 3 } }],
    ])('rejects a request without %s', (_field, params) => {
        expect(tool.schema.safeParse(params).success).toBe(false)
    })

    it('accepts a source dashboard, destination dashboard, and tile', () => {
        expect(tool.schema.safeParse({ id: 1, to_dashboard: 2, tile: { id: 3 } }).success).toBe(true)
    })
})