Files
pipelines/app/test/ui/theme/retro_crt_design_test.dart
T
alan 09bafca21c Merge branch 'main' into worktree-agent-abb9404cc062f4b30
# Conflicts:
#	app/lib/application/app.dart
#	app/lib/application/detached/detached_app.dart
#	app/lib/application/pipelines_root.dart
#	app/lib/features/chat/ui/sessions/chat_color_palette.dart
#	app/lib/ui/theme/app_design.dart
#	app/lib/ui/theme/app_design_tokens.dart
2026-12-04 00:41:42 +00:00

250 lines
9.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_colors.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_color_palette.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/terminal/terminal_glyphs.dart';
import 'package:pipelines_app/features/chat/ui/transcript/terminal/terminal_prompt_line.dart';
import 'package:pipelines_app/ui/text/monospace.dart';
import 'package:pipelines_app/ui/theme/app_design.dart';
import 'package:pipelines_app/ui/theme/app_design_tokens.dart';
import 'package:pipelines_app/ui/theme/crt_palette.dart';
const _crts = [AppDesign.retroCrt, AppDesign.retroCrtAmber];
ThemeData _theme(AppDesign design, Brightness brightness) =>
brightness == Brightness.dark ? design.dark() : design.light();
double _contrast(Color a, Color b) {
final la = a.computeLuminance(), lb = b.computeLuminance();
return (la > lb ? la + 0.05 : lb + 0.05) / (la > lb ? lb + 0.05 : la + 0.05);
}
/// Whether [color] is a shade of [hue] (or a neutral black/white).
bool _ofHue(Color color, double hue) {
final hsl = HSLColor.fromColor(color);
if (hsl.saturation < 0.05 || hsl.lightness < 0.01) return true;
final d = (hsl.hue - hue).abs() % 360;
return (d > 180 ? 360 - d : d) < 3;
}
JsonMap _tool(String id, String status) => {
'type': 'tool',
'id': id,
'name': 'Bash',
'state': {
'status': status,
'input': {'command': 'go test ./...'},
if (status == 'error') 'error': {'message': 'exit status 1'},
},
};
void main() {
test(
'Retro CRT is listed in green and amber, monospace, with stable ids',
() {
expect(AppDesign.fromId('retro-crt'), AppDesign.retroCrt);
expect(AppDesign.fromId('retro-crt-amber'), AppDesign.retroCrtAmber);
expect(AppDesign.retroCrt.label, 'Retro CRT');
expect(AppDesign.retroCrtAmber.label, 'Retro CRT (amber)');
expect(AppDesign.retroCrt.crtPhosphor, CrtPhosphor.green);
expect(AppDesign.retroCrtAmber.crtPhosphor, CrtPhosphor.amber);
expect(AppDesign.terminal.crtPhosphor, isNull);
for (final design in _crts) {
expect(design.description, isNot(contains('\n')));
expect(design.usesInterfaceFont, isFalse);
expect(
design.dark(fontFamily: 'Inter').textTheme.bodyMedium?.fontFamily,
appMonospaceFamily,
);
}
// The green tube's text is the classic #33FF66.
final green = CrtRamp(CrtPhosphor.green, Brightness.dark).text;
expect(green.toARGB32(), 0xFF33FF66);
},
);
test('tokens: Terminal structure, square corners, phosphor frames, one '
'hue, a static screen pattern', () {
for (final design in _crts) {
for (final brightness in Brightness.values) {
final theme = _theme(design, brightness);
final tokens = theme.extension<AppDesignTokens>()!;
expect(tokens.design, design);
expect(tokens.transcript, TranscriptStyle.prompt);
expect(tokens.selectedRow, SelectedRowStyle.framed);
expect(tokens.composer, ComposerStyle.pill);
expect(tokens.radius, 0);
expect(tokens.flat, isTrue);
expect(tokens.frame, theme.colorScheme.outlineVariant);
expect(tokens.monochrome, theme.colorScheme.onSurface);
// Live switches snap: a blended glow hung skwasm.
expect(tokens.snapSwitch, isTrue);
expect(tokens.screen, isNotNull);
expect(tokens.ansi, isNotNull);
final dialog = theme.dialogTheme.shape! as RoundedRectangleBorder;
expect(dialog.borderRadius, BorderRadius.zero);
final tooltip = theme.tooltipTheme.decoration! as BoxDecoration;
expect(tooltip.borderRadius, BorderRadius.zero);
}
}
// Every other design keeps no pattern and full colour.
for (final design in AppDesign.values) {
if (_crts.contains(design)) continue;
final tokens =
design.dark().extension<AppDesignTokens>() ??
AppDesignTokens.standard;
expect(tokens.screen, isNull);
expect(tokens.monochrome, isNull);
}
});
test('every colour is a shade of the phosphor; contrast stays high', () {
for (final design in _crts) {
final hue = design.crtPhosphor!.hue;
for (final brightness in Brightness.values) {
final scheme = _theme(design, brightness).colorScheme;
for (final color in [
scheme.primary,
scheme.secondary,
scheme.tertiary,
scheme.error,
scheme.surface,
scheme.surfaceContainerHighest,
scheme.onSurface,
scheme.onSurfaceVariant,
scheme.outline,
scheme.outlineVariant,
scheme.primaryContainer,
scheme.errorContainer,
]) {
expect(_ofHue(color, hue), isTrue, reason: '$design $color');
}
final page = scheme.surface;
expect(_contrast(scheme.onSurface, page), greaterThan(7));
expect(_contrast(scheme.onSurfaceVariant, page), greaterThan(4.5));
expect(_contrast(scheme.outline, page), greaterThan(1.8));
// Statuses step in brightness: success (text), warning, error.
final ansi = _theme(
design,
brightness,
).extension<AppDesignTokens>()!.ansi!;
final steps = [
ansi.green,
ansi.yellow,
ansi.red,
].map((c) => _contrast(c, page)).toList();
expect(steps[1], greaterThan(steps[0] * 1.15), reason: '$design');
expect(steps[2], greaterThan(steps[1] * 1.1), reason: '$design');
}
}
});
test('dark text glows with one small shadow; light text has none', () {
for (final design in _crts) {
final dark = design.dark().textTheme;
for (final style in [dark.bodyMedium, dark.titleLarge, dark.labelSmall]) {
final shadows = style!.shadows!;
expect(shadows, hasLength(1));
expect(shadows.single.blurRadius, lessThanOrEqualTo(4));
expect(shadows.single.offset, Offset.zero);
expect(shadows.single.color.a, lessThan(0.5));
}
expect(design.light().textTheme.bodyMedium!.shadows, isNull);
}
expect(AppDesign.terminal.dark().textTheme.bodyMedium!.shadows, isNull);
});
testWidgets('chat colours become distinct brightness steps of the phosphor', (
tester,
) async {
for (final design in _crts) {
for (final brightness in Brightness.values) {
final theme = _theme(design, brightness);
late BuildContext context;
await tester.pumpWidget(
MaterialApp(
// A fresh app per theme: no theme animation from the last one.
key: UniqueKey(),
theme: theme,
home: Builder(
builder: (c) {
context = c;
return const SizedBox();
},
),
),
);
final page = theme.colorScheme.surface;
final shades = [
for (final name in chatColorNames) chatColorShade(context, name)!,
];
expect(shades.toSet(), hasLength(chatColorNames.length));
for (final shade in shades) {
expect(_ofHue(shade, design.crtPhosphor!.hue), isTrue);
expect(_contrast(shade, page), greaterThan(3));
}
final lum = [for (final s in shades) s.computeLuminance()];
for (var i = 1; i < lum.length; i++) {
expect(lum[i], isNot(lum[i - 1]));
}
expect(chatColorShade(context, null), isNull);
}
}
});
testWidgets('failed and running tool lines take their own glyph without '
'hue; Terminal keeps its dots', (tester) async {
final row = transcriptRows([
{
'id': 'a1',
'role': 'assistant',
'parts': [
_tool('t1', 'completed'),
_tool('t2', 'error'),
_tool('t3', 'running'),
{'type': 'text', 'text': 'Done.'},
],
},
]).single;
Finder glyph(TerminalGlyphKind kind) =>
find.byWidgetPredicate((w) => w is TerminalGlyph && w.kind == kind);
for (final (design, cross, ring) in [
(AppDesign.retroCrt, 1, 1),
(AppDesign.terminal, 0, 0),
]) {
await tester.pumpWidget(
MaterialApp(
theme: design.dark(),
home: Scaffold(body: ChatMessageView(row: row)),
),
);
await tester.pumpAndSettle();
expect(glyph(TerminalGlyphKind.cross), findsNWidgets(cross));
expect(glyph(TerminalGlyphKind.ring), findsNWidgets(ring));
}
});
testWidgets('the operator turn is Terminal\'s prompt line', (tester) async {
final row = transcriptRows([
{
'id': 'u1',
'role': 'user',
'parts': [
{'type': 'text', 'text': 'Fix it.'},
],
},
]).single;
await tester.pumpWidget(
MaterialApp(
theme: AppDesign.retroCrt.dark(),
themeMode: ThemeMode.dark,
home: Scaffold(body: ChatMessageView(row: row)),
),
);
expect(find.byType(TerminalPromptLine), findsOneWidget);
});
}