|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { expect, test, type Page, type Locator } from '@playwright/test' |
| 10 | + |
| 11 | +import { oxqlQueries } from './oxql-queries' |
| 12 | + |
| 13 | +const runQuery = async (page: Page, query?: string) => { |
| 14 | + if (query !== undefined) await page.getByRole('textbox').fill(query) |
| 15 | + await page.getByRole('button', { name: 'Run query' }).click() |
| 16 | + |
| 17 | + const loading = page.getByLabel('Chart loading') |
| 18 | + await expect(loading).toBeVisible() |
| 19 | + await expect(loading).toBeHidden() |
| 20 | + await expect(page.getByText('Query failed')).toBeHidden() |
| 21 | +} |
| 22 | + |
| 23 | +test.beforeEach(async ({ page }) => { |
| 24 | + await page.goto('/system/oxql') |
| 25 | + await expect(page.getByRole('heading', { name: 'OxQL Explorer' })).toBeVisible() |
| 26 | +}) |
| 27 | + |
| 28 | +test('unaligned multi-table query renders a chart per series', async ({ page }) => { |
| 29 | + await runQuery(page, oxqlQueries.unalignedTables) |
| 30 | + |
| 31 | + // Unaligned queries get you a chart for every series in the result, splitting |
| 32 | + // up tables (since each list of values isn't aligned with the others!) |
| 33 | + await expect(page.getByRole('figure')).toHaveCount(4) // product of table count and fields-per-table |
| 34 | + await expect( |
| 35 | + page.getByRole('figure', { name: 'hardware_component:temperature' }) |
| 36 | + ).toHaveCount(2) |
| 37 | + await expect( |
| 38 | + page.getByRole('figure', { name: 'hardware_component:sensor_error_count' }) |
| 39 | + ).toHaveCount(2) |
| 40 | +}) |
| 41 | + |
| 42 | +const getLegendText = async (locator: Locator): Promise<string[]> => |
| 43 | + locator.getByRole('listitem').allTextContents() |
| 44 | + |
| 45 | +test('aligned multi-table query renders a chart per table', async ({ page }) => { |
| 46 | + await runQuery(page, oxqlQueries.bytesSentAndReceived) |
| 47 | + |
| 48 | + const figures = page.getByRole('figure') |
| 49 | + // Aligned tab |
| 50 | + await expect(figures).toHaveCount(2) // number of tables in query |
| 51 | + const first = figures.first() |
| 52 | + |
| 53 | + // On aligned queries, there's one chart per table queried, and one line (and |
| 54 | + // legend item) per field combination. The legend item depends on mock data, |
| 55 | + // so we just snapshot |
| 56 | + const firstLegendText = await getLegendText(first) |
| 57 | + expect(firstLegendText).toEqual([ |
| 58 | + // depends on whatever mock data returns |
| 59 | + 'instance_id: 935499b3-fd96-432a-9c21-83a3dc1eece4', |
| 60 | + 'instance_id: b5946edc-5bed-4597-88ab-9a8beb9d32a4', |
| 61 | + ]) |
| 62 | + |
| 63 | + const all = await figures.all() |
| 64 | + for (let i = 1; i < all.length; i += 1) { |
| 65 | + // Every chart should have the same sequence of fields, even if the actual |
| 66 | + // combinations are dynamic |
| 67 | + expect(await getLegendText(all[i])).toEqual(firstLegendText) |
| 68 | + } |
| 69 | +}) |
| 70 | + |
| 71 | +test('joined query renders a chart per instance with a legend line per metric', async ({ |
| 72 | + page, |
| 73 | +}) => { |
| 74 | + await runQuery(page, oxqlQueries.multiJoinedTables) |
| 75 | + |
| 76 | + const figures = page.getByRole('figure') |
| 77 | + // Joined queries are an inversion of aligned queries: they have one chart per |
| 78 | + // _field combination,_ and one line/legend item per table in the join |
| 79 | + await expect(figures).toHaveCount(3) // depends on mock data |
| 80 | + const first = figures.first() |
| 81 | + await expect(first.getByRole('listitem')).toHaveText([ |
| 82 | + 'sled_data_link:bytes_sent', |
| 83 | + 'sled_data_link:errors_sent', |
| 84 | + 'sled_data_link:bytes_received', |
| 85 | + 'sled_data_link:errors_received', |
| 86 | + ]) |
| 87 | +}) |
| 88 | + |
| 89 | +test('"Drop first point" appears only for cumulative-derived charts', async ({ page }) => { |
| 90 | + const dropFirst = page.getByLabel('Drop first point') |
| 91 | + |
| 92 | + // a plain gauge is never cumulative, so there's no giant first point to drop |
| 93 | + await runQuery(page, oxqlQueries.basicTctl) |
| 94 | + await expect(dropFirst).toBeHidden() |
| 95 | + |
| 96 | + // joined/aligned tables may derive from cumulatives, so the option shows up |
| 97 | + // TODO: if you know the schemas, you can check which tables are cumulative! |
| 98 | + await runQuery(page, oxqlQueries.multiJoinedTables) |
| 99 | + await expect(dropFirst).toBeChecked() |
| 100 | + |
| 101 | + await dropFirst.uncheck() |
| 102 | + await expect(page.getByRole('figure')).toHaveCount(3) |
| 103 | +}) |
| 104 | + |
| 105 | +test('empty query is blocked by client-side validation', async ({ page }) => { |
| 106 | + const textbox = page.getByRole('textbox') |
| 107 | + await textbox.fill('') |
| 108 | + await page.getByRole('button', { name: 'Run query' }).click() |
| 109 | + |
| 110 | + await expect(textbox).toHaveAttribute('aria-invalid', 'true') |
| 111 | + await expect(page.getByText('Enter a query').first()).toBeVisible() |
| 112 | + await expect(page.getByRole('figure')).toHaveCount(0) |
| 113 | +}) |
| 114 | + |
| 115 | +test('a query the backend rejects surfaces an error instead of a chart', async ({ |
| 116 | + page, |
| 117 | +}) => { |
| 118 | + await page.getByRole('textbox').fill('junk junk junk!') |
| 119 | + await page.getByRole('button', { name: 'Run query' }).click() |
| 120 | + |
| 121 | + await expect(page.getByText('Query failed')).toBeVisible() |
| 122 | + await expect(page.getByRole('figure')).toHaveCount(0) |
| 123 | +}) |
0 commit comments