Skip to content

Commit d84d710

Browse files
authored
Alert list class search (#3393)
Alert list class filtering requires an exact match to a known schema, so we need to do a lot of client-side validation. On the bright side, we now have a lot of client-side validation!
1 parent 7988361 commit d84d710

14 files changed

Lines changed: 743 additions & 111 deletions

File tree

‎app/api/util.spec.ts‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,68 @@ import {
1919

2020
describe('subscriptionRegex', () => {
2121
it('matches exact class names', () => {
22-
expect(subscriptionRegex('instance.create').test('instance.create')).toBe(true)
23-
expect(subscriptionRegex('instance.create').test('instance.created')).toBe(false)
22+
expect(subscriptionRegex('instance.create')?.test('instance.create')).toBe(true)
23+
expect(subscriptionRegex('instance.create')?.test('instance.created')).toBe(false)
2424
})
2525

2626
it('* matches exactly one segment', () => {
2727
const re = subscriptionRegex('disk.*')
28-
expect(re.test('disk.create')).toBe(true)
29-
expect(re.test('disk.snapshot.create')).toBe(false)
30-
expect(re.test('disk')).toBe(false)
28+
expect(re?.test('disk.create')).toBe(true)
29+
expect(re?.test('disk.snapshot.create')).toBe(false)
30+
expect(re?.test('disk')).toBe(false)
3131
})
3232

3333
it('* can appear at the head', () => {
3434
const re = subscriptionRegex('*.create')
35-
expect(re.test('disk.create')).toBe(true)
36-
expect(re.test('instance.create')).toBe(true)
37-
expect(re.test('instance.ephemeral_ip.create')).toBe(false)
35+
expect(re?.test('disk.create')).toBe(true)
36+
expect(re?.test('instance.create')).toBe(true)
37+
expect(re?.test('instance.ephemeral_ip.create')).toBe(false)
3838
})
3939

4040
it('* can appear mid-string', () => {
4141
const re = subscriptionRegex('hardware.*.insert')
42-
expect(re.test('hardware.sled.insert')).toBe(true)
43-
expect(re.test('hardware.power_shelf.psu.insert')).toBe(false)
44-
expect(re.test('hardware.insert')).toBe(false)
42+
expect(re?.test('hardware.sled.insert')).toBe(true)
43+
expect(re?.test('hardware.power_shelf.psu.insert')).toBe(false)
44+
expect(re?.test('hardware.insert')).toBe(false)
4545
})
4646

4747
it('** matches one or more segments', () => {
4848
const re = subscriptionRegex('hardware.**')
49-
expect(re.test('hardware.power_shelf.psu.insert')).toBe(true)
50-
expect(re.test('hardware.psu')).toBe(true)
51-
expect(re.test('hardware')).toBe(false)
49+
expect(re?.test('hardware.power_shelf.psu.insert')).toBe(true)
50+
expect(re?.test('hardware.psu')).toBe(true)
51+
expect(re?.test('hardware')).toBe(false)
5252

5353
const suffix = subscriptionRegex('**.delete')
54-
expect(suffix.test('project.delete')).toBe(true)
55-
expect(suffix.test('instance.ephemeral_ip.delete')).toBe(true)
56-
expect(suffix.test('delete')).toBe(false)
54+
expect(suffix?.test('project.delete')).toBe(true)
55+
expect(suffix?.test('instance.ephemeral_ip.delete')).toBe(true)
56+
expect(suffix?.test('delete')).toBe(false)
5757

5858
const infix = subscriptionRegex('hardware.**.insert')
59-
expect(infix.test('hardware.sled.insert')).toBe(true)
60-
expect(infix.test('hardware.power_shelf.psu.insert')).toBe(true)
61-
expect(infix.test('hardware.insert')).toBe(false)
59+
expect(infix?.test('hardware.sled.insert')).toBe(true)
60+
expect(infix?.test('hardware.power_shelf.psu.insert')).toBe(true)
61+
expect(infix?.test('hardware.insert')).toBe(false)
6262
})
6363

6464
it('does not match substrings within a segment', () => {
65-
expect(subscriptionRegex('instance.**').test('silo.instance_quota.hit')).toBe(false)
66-
expect(subscriptionRegex('disk.*').test('bigdisk.create')).toBe(false)
65+
expect(subscriptionRegex('instance.**')?.test('silo.instance_quota.hit')).toBe(false)
66+
expect(subscriptionRegex('disk.*')?.test('bigdisk.create')).toBe(false)
6767
})
6868

6969
it('does not match leading/trailing segments without a star pattern', () => {
70-
expect(subscriptionRegex('instance.**').test('silo.instance.hit')).toBe(false)
71-
expect(subscriptionRegex('**.power_shelf').test('hardware.power_shelf.psu')).toBe(false)
70+
expect(subscriptionRegex('instance.**')?.test('silo.instance.hit')).toBe(false)
71+
expect(subscriptionRegex('**.power_shelf')?.test('hardware.power_shelf.psu')).toBe(
72+
false
73+
)
74+
})
75+
76+
it('never throws on invalid input, including regex metacharacters', () => {
77+
expect(subscriptionRegex('')).toBeNull()
78+
expect(subscriptionRegex('a|b*')).toBeNull()
79+
expect(subscriptionRegex('disk+')).toBeNull()
80+
expect(subscriptionRegex('(*')).toBeNull()
81+
expect(subscriptionRegex('*.{2,}')).toBeNull()
82+
expect(subscriptionRegex('hardware.***')).toBeNull()
83+
expect(subscriptionRegex('hardware..disk')).toBeNull()
7284
})
7385
})
7486

‎app/api/util.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ export const isSubscribableClass = (c: { name: string }) => c.name !== PROBE_ALE
7171

7272
/**
7373
* Convert an alert subscription to a regex matching the class names it covers:
74-
* a `*` segment matches exactly one segment, `**` matches one or more.
74+
* a `*` segment matches exactly one segment, `**` matches one or more. An
75+
* invalid input returns `null`.
7576
* https://github.com/oxidecomputer/omicron/blob/32615a35/nexus/db-model/src/alert_subscription.rs
7677
*/
77-
export function subscriptionRegex(subscription: string) {
78+
export function subscriptionRegex(subscription: string): RegExp | null {
79+
if (!ALERT_SUBSCRIPTION_REGEX.test(subscription)) return null
7880
const pattern = subscription
7981
.split('.')
8082
.map((seg) => (seg === '**' ? '.+' : seg === '*' ? '[^.]+' : seg))
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
import { useState } from 'react'
9+
import { expect, test } from 'vitest'
10+
import { render } from 'vitest-browser-react'
11+
import { page, userEvent } from 'vitest/browser'
12+
13+
import { TextField } from '~/components/form/fields/TextField'
14+
15+
import { FilterPopover } from './FilterPopover'
16+
17+
type Filters = { name: string; owner: string; types: string[] }
18+
19+
function Harness() {
20+
const defaultValues = { name: '', owner: '', types: [] }
21+
const [applied, setApplied] = useState<Filters>(defaultValues)
22+
return (
23+
<>
24+
<FilterPopover
25+
isFetching={false}
26+
resetFieldValues={defaultValues}
27+
lastApplied={applied}
28+
handleSubmit={setApplied}
29+
pluralFilteredItemName="widgets"
30+
>
31+
{(form) => (
32+
<>
33+
<TextField
34+
name="name"
35+
label="Name"
36+
control={form.control}
37+
validate={(value) => (value === 'bad' ? 'No bad names' : undefined)}
38+
/>
39+
<TextField name="owner" label="Owner" control={form.control} />
40+
<button
41+
type="button"
42+
onClick={() => setApplied((prev) => ({ ...prev, types: ['disk'] }))}
43+
>
44+
Apply type filter
45+
</button>
46+
</>
47+
)}
48+
</FilterPopover>
49+
<output aria-label="Applied name">{applied.name}</output>
50+
</>
51+
)
52+
}
53+
54+
const filterButton = () => page.getByRole('button', { name: /^Filter widgets/ })
55+
const form = () => page.getByRole('form', { name: 'Filter widgets' })
56+
const resetButton = () => page.getByRole('button', { name: 'Reset' })
57+
const nameField = () => page.getByRole('textbox', { name: 'Name' })
58+
const applyButton = () => page.getByRole('button', { name: 'Apply' })
59+
const appliedName = () => page.getByRole('status', { name: 'Applied name' })
60+
61+
test('badge counts applied filters', async () => {
62+
await render(<Harness />)
63+
const noneApplied = page.getByRole('button', { name: 'Filter widgets', exact: true })
64+
await expect.element(noneApplied).toBeVisible()
65+
66+
await noneApplied.click()
67+
await page.getByRole('button', { name: 'Apply type filter' }).click()
68+
await userEvent.keyboard('{Escape}')
69+
const oneApplied = page.getByRole('button', { name: 'Filter widgets (1 applied)' })
70+
await expect.element(oneApplied).toBeVisible()
71+
// this is the non-accessible text
72+
await expect.element(oneApplied.getByText('1')).toBeVisible()
73+
74+
await oneApplied.click()
75+
await nameField().fill('abc')
76+
await applyButton().click()
77+
await expect
78+
.element(page.getByRole('button', { name: 'Filter widgets (2 applied)' }))
79+
.toBeVisible()
80+
81+
await filterButton().click()
82+
await page.getByRole('textbox', { name: 'Owner' }).fill('abc')
83+
await applyButton().click()
84+
await expect
85+
.element(page.getByRole('button', { name: 'Filter widgets (3 applied)' }))
86+
.toBeVisible()
87+
})
88+
89+
test('apply submits the values and closes only if there are no validation errors', async () => {
90+
await render(<Harness />)
91+
await filterButton().click()
92+
await nameField().fill('bad')
93+
await applyButton().click()
94+
await expect.element(form().getByText('No bad names')).toBeVisible()
95+
await expect.element(appliedName()).toHaveTextContent('')
96+
97+
await nameField().fill('good')
98+
await applyButton().click()
99+
await expect.element(form()).not.toBeInTheDocument()
100+
await expect.element(appliedName()).toHaveTextContent('good')
101+
})
102+
103+
test('reset applies the reset values', async () => {
104+
await render(<Harness />)
105+
await filterButton().click()
106+
await nameField().fill('abc')
107+
await applyButton().click()
108+
await expect.element(appliedName()).toHaveTextContent('abc')
109+
await filterButton().click()
110+
await resetButton().click()
111+
await expect.element(form()).not.toBeInTheDocument()
112+
await expect.element(appliedName()).toHaveTextContent('')
113+
await expect
114+
.element(page.getByRole('button', { name: 'Filter widgets', exact: true }))
115+
.toBeVisible()
116+
})
117+
118+
test('reset is disabled when nothing is applied', async () => {
119+
await render(<Harness />)
120+
await filterButton().click()
121+
await expect.element(resetButton()).toBeDisabled()
122+
123+
// it's also disabled if you simply restore the default form values manually
124+
await nameField().fill('abc')
125+
await applyButton().click()
126+
await filterButton().click()
127+
await nameField().fill('')
128+
await applyButton().click()
129+
await filterButton().click()
130+
await expect.element(resetButton()).toBeDisabled()
131+
})
132+
133+
test('unapplied edits are discarded on close', async () => {
134+
await render(<Harness />)
135+
await filterButton().click()
136+
await nameField().fill('draft')
137+
await userEvent.keyboard('{Escape}')
138+
await expect.element(form()).not.toBeInTheDocument()
139+
140+
await filterButton().click()
141+
await expect.element(nameField()).toHaveValue('')
142+
})

0 commit comments

Comments
 (0)