Files
supabase/apps/studio/tests/components/AuditLogs/AuditLogs.utils.test.ts
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## Context

_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_

We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.

## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail 
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files 
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
2026-10-23 18:51:27 +08:00

153 lines
4.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import dayjs from 'dayjs'
import type { FilterGroup } from 'ui-patterns/FilterBar'
import { describe, expect, test } from 'vitest'
import { filterByFilterGroup } from '@/components/interfaces/AuditLogs/AuditLogs.utils'
import {
TIMESTAMP_MICROS_PER_MS,
type AuditLog,
} from '@/data/organizations/organization-audit-logs-query'
// Timestamps are in microseconds (e.g. 1777471903844000 = April 2026)
const TS_A = 1777471903844000
const TS_B = 1777471903845000
const TS_C = 1777471903846000
function makeLog(overrides: Partial<AuditLog> = {}): AuditLog {
return {
timestamp: TS_A,
request_id: 'req-1',
action: { name: 'test', method: 'GET', route: '/api/test', status: 200 },
actor: { token_type: 'bearer' },
...overrides,
}
}
const logA = makeLog({ timestamp: TS_A, request_id: 'req-a', project_ref: 'proj-1' })
const logB = makeLog({
timestamp: TS_B,
request_id: 'req-b',
project_ref: 'proj-2',
actor: { token_type: 'bearer', user_id: 'user-1' },
})
const logC = makeLog({
timestamp: TS_C,
request_id: 'req-c',
actor: { token_type: 'bearer', user_id: 'user-2' },
})
describe('timestamp conversion (microseconds → milliseconds)', () => {
test('dividing by TIMESTAMP_MICROS_PER_MS produces the correct date', () => {
expect(dayjs(TS_A / TIMESTAMP_MICROS_PER_MS).year()).toBe(2026)
})
})
describe('filterByFilterGroup', () => {
const project = (value: string) => ({
propertyName: 'project',
operator: '=',
value,
})
const user = (value: string) => ({
propertyName: 'user',
operator: '=',
value,
})
const date = {
propertyName: 'date',
operator: '=',
value: '2026-04-29T00:00:00.000Z – 2026-04-30T00:00:00.000Z',
}
const logs = [logA, logB, logC]
const requestIds = (group: FilterGroup) =>
filterByFilterGroup(logs, group).map((l) => l.request_id)
test('returns all logs when there are no conditions', () => {
expect(requestIds({ logicalOperator: 'AND', conditions: [] })).toEqual([
'req-a',
'req-b',
'req-c',
])
})
test('OR matches logs from any of the projects', () => {
expect(
requestIds({ logicalOperator: 'OR', conditions: [project('proj-1'), project('proj-2')] })
).toEqual(['req-a', 'req-b'])
})
test('AND of two different projects matches nothing', () => {
expect(
requestIds({ logicalOperator: 'AND', conditions: [project('proj-1'), project('proj-2')] })
).toEqual([])
})
test('evaluates nested groups: date AND (project A OR project B)', () => {
expect(
requestIds({
logicalOperator: 'AND',
conditions: [
date,
{ logicalOperator: 'OR', conditions: [project('proj-1'), project('proj-2')] },
],
})
).toEqual(['req-a', 'req-b'])
})
test('ignores the date condition since the API already applies it', () => {
expect(requestIds({ logicalOperator: 'AND', conditions: [date] })).toEqual([
'req-a',
'req-b',
'req-c',
])
expect(requestIds({ logicalOperator: 'OR', conditions: [date, project('proj-1')] })).toEqual([
'req-a',
])
})
test('ignores chips that have no value yet', () => {
expect(requestIds({ logicalOperator: 'AND', conditions: [project('')] })).toEqual([
'req-a',
'req-b',
'req-c',
])
expect(
requestIds({ logicalOperator: 'OR', conditions: [project(''), project('proj-1')] })
).toEqual(['req-a'])
})
test('ignores an empty nested group', () => {
expect(
requestIds({
logicalOperator: 'AND',
conditions: [project('proj-1'), { logicalOperator: 'OR', conditions: [] }],
})
).toEqual(['req-a'])
})
test('excludes logs without a project_ref when a project is selected', () => {
expect(requestIds({ logicalOperator: 'OR', conditions: [project('proj-1')] })).not.toContain(
'req-c'
)
})
test('OR matches logs from any of the users', () => {
expect(
requestIds({ logicalOperator: 'OR', conditions: [user('user-1'), user('user-2')] })
).toEqual(['req-b', 'req-c'])
})
test('excludes logs without a matching actor when a user is selected', () => {
expect(requestIds({ logicalOperator: 'AND', conditions: [user('user-unknown')] })).toEqual([])
})
test('combines user and project: user AND project', () => {
expect(
requestIds({ logicalOperator: 'AND', conditions: [user('user-1'), project('proj-2')] })
).toEqual(['req-b'])
expect(
requestIds({ logicalOperator: 'AND', conditions: [user('user-2'), project('proj-2')] })
).toEqual([])
})
})