Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions apps/web/src/components/blueprint/harness-slide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import {
XCircle,
} from '@phosphor-icons/react';
import { useMemo, useState } from 'react';
import {
REQUIREMENT_SEVERITIES,
sortRequirementsBySeverity,
type RequirementSeverity,
} from '@/lib/requirement-severity';
import type { HarnessSnapshot } from './use-project-stream';

type Requirement = {
id: string;
excerpt: string | null;
description: string;
type: 'explicit' | 'hidden';
severity: 'critical' | 'high' | 'medium' | 'low';
severity: RequirementSeverity;
// True once a user has created or edited this row through the UI — it is then
// manually owned and the worker's auto-derivation leaves it alone. "Reset to
// auto" clears it.
Expand All @@ -49,8 +54,6 @@ type StagedOp =

type Verdict = 'pass' | 'fail' | 'inconclusive' | 'unverified';

const SEVERITIES = ['critical', 'high', 'medium', 'low'] as const;

const SEVERITY_BADGE: Record<string, string> = {
critical: 'bg-red-100 text-red-700',
high: 'bg-orange-100 text-orange-700',
Expand Down Expand Up @@ -189,7 +192,7 @@ function RequirementForm({
onChange={(e) => setSeverity(e.target.value as Requirement['severity'])}
className="rounded-md border border-slate-200 px-2 py-1.5 text-sm"
>
{SEVERITIES.map((s) => (
{REQUIREMENT_SEVERITIES.map((s) => (
<option key={s} value={s}>
{s[0].toUpperCase() + s.slice(1)}
</option>
Expand Down Expand Up @@ -281,13 +284,19 @@ export function HarnessSlide({
}, [effective, verdicts]);

const grouped = useMemo(
() =>
SEVERITIES.map((sev) => ({
() => {
const visible = sortRequirementsBySeverity(
effective.filter(
(requirement) =>
filter === 'all' || verdicts.get(requirement.id) === filter
)
);

return REQUIREMENT_SEVERITIES.map((sev) => ({
sev,
items: effective.filter(
(r) => r.severity === sev && (filter === 'all' || verdicts.get(r.id) === filter)
),
})).filter((g) => g.items.length > 0),
items: visible.filter((requirement) => requirement.severity === sev),
})).filter((group) => group.items.length > 0);
},
[effective, filter, verdicts]
);

Expand Down
39 changes: 39 additions & 0 deletions apps/web/src/lib/requirement-severity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest';
import { sortRequirementsBySeverity } from './requirement-severity';

describe('sortRequirementsBySeverity', () => {
it('orders requirements from critical to low', () => {
const requirements = [
{ id: 'low', severity: 'low' as const },
{ id: 'medium', severity: 'medium' as const },
{ id: 'critical', severity: 'critical' as const },
{ id: 'high', severity: 'high' as const },
];

expect(sortRequirementsBySeverity(requirements).map(({ id }) => id)).toEqual([
'critical',
'high',
'medium',
'low',
]);
});

it('preserves source data, input order within a severity, and the input array', () => {
const requirements = [
{ id: 'high-first', description: 'first', severity: 'high' as const },
{ id: 'low', description: 'last', severity: 'low' as const },
{ id: 'high-second', description: 'second', severity: 'high' as const },
];
const original = [...requirements];
const sorted = sortRequirementsBySeverity(requirements);

expect(sorted).toEqual([
requirements[0],
requirements[2],
requirements[1],
]);
expect(requirements).toEqual(original);
expect(sorted[0]).toBe(requirements[0]);
expect(sorted[1]).toBe(requirements[2]);
});
});
24 changes: 24 additions & 0 deletions apps/web/src/lib/requirement-severity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const REQUIREMENT_SEVERITIES = [
'critical',
'high',
'medium',
'low',
] as const;

export type RequirementSeverity = (typeof REQUIREMENT_SEVERITIES)[number];

const SEVERITY_RANK: Record<RequirementSeverity, number> = {
critical: 0,
high: 1,
medium: 2,
low: 3,
};

export function sortRequirementsBySeverity<
T extends { severity: RequirementSeverity },
>(requirements: readonly T[]): T[] {
return [...requirements].sort(
(left, right) =>
SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity],
);
}
Loading