From 8d02210a620a159de66de40633e8997dcc33631f Mon Sep 17 00:00:00 2001 From: liuhy Date: Sat, 25 Jul 2026 17:53:44 -0700 Subject: [PATCH] fix: make audit mock search null safe --- web/src/services/opsService.test.ts | 54 +++++++++++++++++++++++++++++ web/src/services/opsService.ts | 10 ++++-- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 web/src/services/opsService.test.ts diff --git a/web/src/services/opsService.test.ts b/web/src/services/opsService.test.ts new file mode 100644 index 00000000..face7457 --- /dev/null +++ b/web/src/services/opsService.test.ts @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { afterEach, describe, expect, it, vi } from 'vitest'; +import type { AuditRecord } from '../api/ops'; +import { mockAuditRecords } from '../mock/audit'; + +vi.mock('../config', () => ({ API_BASE_URL: '/api', USE_MOCK: true })); + +describe('ops service mock audit records', () => { + const auditRecords = mockAuditRecords as unknown as AuditRecord[]; + const insertedRecords: AuditRecord[] = []; + + afterEach(() => { + for (const record of insertedRecords.splice(0)) { + const index = auditRecords.findIndex((item) => item.id === record.id); + if (index >= 0) auditRecords.splice(index, 1); + } + }); + + it('searches records safely when optional text fields are missing', async () => { + const record = { + id: 'audit-null-safe', + timestamp: '2026-07-26 10:00:00', + operator: null, + operationType: 'DIAGNOSE', + target: null, + detail: 'Describe gRPC client connection', + ipAddress: '127.0.0.1', + result: 'success', + } as unknown as AuditRecord; + insertedRecords.push(record); + auditRecords.push(record); + + const { listAuditRecords } = await import('./opsService'); + const result = await listAuditRecords({ search: 'grpc client', pageSize: 100 }); + + expect(result.items.map((item) => item.id)).toContain('audit-null-safe'); + }); +}); diff --git a/web/src/services/opsService.ts b/web/src/services/opsService.ts index 4563d72e..4ce8931a 100644 --- a/web/src/services/opsService.ts +++ b/web/src/services/opsService.ts @@ -8,6 +8,10 @@ import { systemAlerts as mockSystemAlerts } from '../mock/dashboard'; let auditRecordsState = mockAuditRecords as unknown as AuditRecord[]; const alertRulesState = mockAlertRules as unknown as AlertRule[]; +function includesIgnoreCase(value: string | null | undefined, search: string): boolean { + return (value ?? '').toLowerCase().includes(search); +} + export async function listAlertRules(): Promise { if (USE_MOCK) return alertRulesState; return opsApi.listAlertRules(); @@ -97,9 +101,9 @@ export async function listAuditRecords(params: AuditQuery = {}): Promise