Files
pipelines/app/test/features/attention/attention_models_test.dart
T
alan 38f8576c84 feat(app): add the pure native attention model
Port the browser's Needs-you list, dismissal bookkeeping and attention
transition ledger as widget-free Dart under lib/features/attention/domain/.
Includes the local-day window, export-wait exclusion, {runId}:{kind}
identities, sorted dismissal storage and focused regressions.
2026-10-21 22:14:04 +01:00

335 lines
9.9 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/attention/domain/attention_models.dart';
Map<String, dynamic> run([Map<String, dynamic> overrides = const {}]) => {
'id': 1,
'name': 'Plan and land',
'title': null,
'status': 'failed',
'created_at': '2026-10-19T10:00:00Z',
'finished_at': '2026-10-19T10:05:00Z',
'diagnostic': '',
...overrides,
};
void main() {
group('qualification', () {
test('only waiting and failed qualify; every other status asks nobody', () {
expect(attentionKindOf('waiting'), AttentionKind.decision);
expect(attentionKindOf('failed'), AttentionKind.failed);
for (final status in [
'queued',
'running',
'succeeded',
'canceled',
'cancelled',
'interrupted',
'active',
'blocked',
'skipped',
'unknown',
'',
' ',
null,
'FAILED?',
]) {
expect(
attentionKindOf(status),
isNull,
reason: 'status ${status ?? 'null'} must not qualify',
);
}
});
test('trims and lowercases the server words', () {
expect(attentionKindOf(' Waiting '), AttentionKind.decision);
expect(attentionKindOf('Failed\n'), AttentionKind.failed);
expect(attentionKindOf('WAITING'), AttentionKind.decision);
});
test('ranks are Decision 0 then Failed 1', () {
expect(AttentionKind.decision.rank, 0);
expect(AttentionKind.failed.rank, 1);
});
test('waiting excludes audit/export handoffs, not resource waits', () {
expect(
attentionKindForRun(run({'status': 'waiting'})),
AttentionKind.decision,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': 4}),
),
isNull,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': ' 2 '}),
),
isNull,
);
expect(
attentionKindForRun(
run({'status': 'waiting', 'export_pending_sequence': 0}),
),
AttentionKind.decision,
);
// web/src/attention.js and Attention.kt never consult resource_wait; the
// server retired it, so a stale value must not hide a real decision.
for (final stale in [
{'resource': 'device-1'},
'device-1',
true,
1,
]) {
expect(
attentionKindForRun(
run({'status': 'waiting', 'resource_wait': stale}),
),
AttentionKind.decision,
reason: 'resource_wait $stale must not suppress a decision',
);
}
// A failed run stays failed even if a stale export field survived.
expect(
attentionKindForRun(
run({'status': 'failed', 'export_pending_sequence': 4}),
),
AttentionKind.failed,
);
});
test(
'nonqualifying runs are dropped entirely, including malformed ids',
() {
final entries = attentionEntries([
run({'id': 1, 'status': 'succeeded'}),
run({'id': 2, 'status': 'running'}),
run({'id': 3, 'status': 'failed'}),
run({'id': 4, 'status': 'queued'}),
run({'id': 'not-a-number', 'status': 'failed'}),
run({'id': 0, 'status': 'failed'}),
]);
expect(entries.map((entry) => entry.runId), [3]);
expect(attentionEntries(null), isEmpty);
},
);
});
group('identity and wording', () {
test('identity is exactly {runId}:{kind}', () {
expect(attentionKey(440, AttentionKind.failed), '440:Failed');
expect(attentionKey(12, AttentionKind.decision), '12:Decision');
});
test('label is the trimmed title else the pipeline name', () {
expect(attentionEntryFor(run({'title': ' My run '}))!.label, 'My run');
expect(attentionEntryFor(run({'title': ' '}))!.label, 'Plan and land');
expect(attentionEntryFor(run({'title': null}))!.label, 'Plan and land');
});
test(
'detail is the decision line or the trimmed diagnostic else Run failed',
() {
expect(
attentionDetail(AttentionKind.decision, run({'status': 'waiting'})),
'Waiting for your decision',
);
expect(
attentionDetail(
AttentionKind.failed,
run({'diagnostic': ' boom '}),
),
'boom',
);
expect(
attentionDetail(AttentionKind.failed, run({'diagnostic': ' '})),
'Run failed',
);
expect(
attentionDetail(AttentionKind.failed, run({'diagnostic': null})),
'Run failed',
);
},
);
test('row description is exactly {pipeline} · {detail}', () {
final entry = attentionEntryFor(
run({'title': 'T', 'diagnostic': 'failed hard'}),
)!;
expect(attentionRowDescription(entry), 'Plan and land · failed hard');
});
test('badge label is bare Notifications when empty, else the count', () {
expect(attentionBadgeLabel(0), 'Notifications');
expect(attentionBadgeLabel(-3), 'Notifications');
expect(attentionBadgeLabel(1), 'Notifications, 1 needing attention');
expect(attentionBadgeLabel(171), 'Notifications, 171 needing attention');
});
test('the entry keeps the whole summary, including unknown fields', () {
final source = run({
'unknown_future_field': {'keep': true},
'id': 7,
});
final entry = attentionEntryFor(source)!;
expect(entry.run['unknown_future_field'], {'keep': true});
expect(entry.run, same(source));
});
});
group('ordering', () {
test('kind rank, then recency descending, then run id descending', () {
final entries = attentionEntries([
run({
'id': 10,
'status': 'failed',
'finished_at': '2026-10-19T10:00:00Z',
}),
run({
'id': 11,
'status': 'waiting',
'finished_at': '2026-10-19T09:00:00Z',
}),
run({
'id': 12,
'status': 'failed',
'finished_at': '2026-10-19T11:00:00Z',
}),
run({
'id': 13,
'status': 'waiting',
'finished_at': '2026-10-19T08:00:00Z',
}),
]);
expect(entries.map((entry) => entry.runId), [11, 13, 12, 10]);
});
test('recency falls back to created_at when finished_at is blank', () {
final entries = attentionEntries([
run({
'id': 1,
'status': 'failed',
'finished_at': '',
'created_at': '2026-10-19T12:00:00Z',
}),
run({
'id': 2,
'status': 'failed',
'finished_at': '2026-10-19T11:00:00Z',
}),
]);
expect(entries.map((entry) => entry.runId), [1, 2]);
});
test('equal kind and recency tie by run id descending and stay stable', () {
final rows = [
run({'id': 3, 'finished_at': '2026-10-19T10:00:00Z'}),
run({'id': 9, 'finished_at': '2026-10-19T10:00:00Z'}),
run({'id': 5, 'finished_at': '2026-10-19T10:00:00Z'}),
];
expect(attentionEntries(rows).map((entry) => entry.runId), [9, 5, 3]);
expect(attentionEntries(rows.reversed).map((entry) => entry.runId), [
9,
5,
3,
]);
});
});
group('local-day window', () {
final since = DateTime.utc(2026, 10, 20);
final rows = [
run({'id': 1, 'finished_at': '2026-10-19T21:33:27Z'}),
run({'id': 2, 'finished_at': '2026-10-20T10:00:00Z'}),
];
test('startOfLocalDay is local midnight, injectable for tests', () {
final now = DateTime(2026, 10, 20, 15, 4, 5, 6);
final start = startOfLocalDay(now: now);
expect(start, DateTime(2026, 10, 20));
expect(start.hour, 0);
expect(start.minute, 0);
expect(start.second, 0);
expect(start.millisecond, 0);
expect(start.isBefore(now), isTrue);
expect(start.toLocal(), start);
});
test('a cutoff drops older work and keeps newer work', () {
expect(attentionEntries(rows, since: since).map((entry) => entry.runId), [
2,
]);
});
test('no cutoff, and an epoch-zero cutoff, show everything', () {
expect(attentionEntries(rows).map((entry) => entry.runId), [2, 1]);
expect(
attentionEntries(
rows,
since: DateTime.fromMillisecondsSinceEpoch(0),
).map((entry) => entry.runId),
[2, 1],
);
});
test('the cutoff applies to waiting decisions as well as failures', () {
expect(
attentionEntries([
run({
'id': 3,
'status': 'waiting',
'finished_at': '2026-10-19T09:00:00Z',
}),
], since: since),
isEmpty,
);
});
test('unparseable or blank recency is kept, as in the browser', () {
for (final recency in ['', ' ', 'not a timestamp']) {
final kept = attentionEntries([
run({'id': 4, 'finished_at': recency, 'created_at': recency}),
], since: since);
expect(
kept.map((entry) => entry.runId),
[4],
reason: 'recency ${recency.isEmpty ? '<blank>' : recency} is kept',
);
}
});
test('finished_at is the recency the cutoff reads, else created_at', () {
expect(
attentionEntries([
run({
'id': 5,
'created_at': '2026-10-18T09:00:00Z',
'finished_at': '2026-10-20T09:00:00Z',
}),
], since: since).map((entry) => entry.runId),
[5],
);
expect(
attentionEntries([
run({
'id': 6,
'created_at': '2026-10-18T09:00:00Z',
'finished_at': '',
}),
], since: since),
isEmpty,
);
});
test('dismissal and the cutoff compose', () {
final dismissed = attentionDismiss(const {}, '2:Failed');
expect(
attentionEntries(rows, dismissed: dismissed, since: since),
isEmpty,
);
});
});
}