Show discreet local timestamps on chat messages

This commit is contained in:
2026-11-05 22:00:13 +00:00
parent 5b0c46b27a
commit 9180e84109
8 changed files with 311 additions and 1 deletions
@@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_copy_button.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_timestamp.dart';
JsonMap _row(List<JsonMap> messages) => transcriptRows(messages).single;
@@ -59,6 +61,150 @@ void _phone(WidgetTester tester) {
}
void main() {
test('folded replies retain the first valid recorded epoch time', () {
JsonMap message(String id, Object? time) => {
'id': id,
'role': 'assistant',
'created_at': time,
'parts': [
{'type': 'text', 'text': id},
],
};
final first = DateTime.utc(2026, 10, 20, 8, 12).millisecondsSinceEpoch;
final messages = [message('a1', first), message('a2', first + 60000)];
expect(transcriptRows(messages).single['created_at'], first);
expect(transcriptRows(messages, running: true).single['created_at'], first);
expect(
transcriptRows([message('bad', 0), ...messages]).single['created_at'],
first,
);
for (final invalid in [
null,
0,
-1,
double.nan,
double.infinity,
1.5,
8640000000000001,
'2026-10-20',
]) {
expect(chatMessageCreatedAt({'created_at': invalid}), isNull);
}
});
testWidgets(
'Cupertino timestamp supports large text without Material localizations',
(tester) async {
_phone(tester);
await tester.pumpWidget(
_host(
_row([
{
'id': 'u1',
'role': 'user',
'created_at': DateTime.utc(
2026,
10,
20,
8,
12,
).millisecondsSinceEpoch,
'parts': [
{'type': 'text', 'text': 'Question'},
],
},
]),
cupertino: true,
scale: 2,
),
);
await tester.pumpAndSettle();
expect(find.byType(ChatMessageTimestamp), findsOneWidget);
expect(tester.takeException(), isNull);
},
);
testWidgets('shows a local dated timestamp but keeps it out of copy', (
tester,
) async {
_phone(tester);
final rows = transcriptRows([
{
'id': 'u1',
'role': 'user',
'created_at': DateTime.utc(2026, 10, 20, 8, 12).millisecondsSinceEpoch,
'parts': [
{'type': 'text', 'text': 'Question'},
],
},
{
'id': 'a1',
'role': 'assistant',
'created_at': DateTime.utc(2026, 10, 20, 8, 13).millisecondsSinceEpoch,
'parts': [
{'type': 'text', 'text': 'Answer'},
],
},
]);
// Use the actual localized formatter rather than asserting a machine's
// timezone or 12/24-hour preference.
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(brightness: Brightness.dark),
home: Scaffold(
body: ListView(
children: [for (final row in rows) ChatMessageView(row: row)],
),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(ChatMessageTimestamp), findsNWidgets(2));
final context = tester.element(find.byType(ChatMessageTimestamp).first);
final local = DateTime.utc(2026, 10, 20, 8, 12).toLocal();
final l10n = MaterialLocalizations.of(context);
final expected =
'${l10n.formatCompactDate(local)} · ${l10n.formatTimeOfDay(TimeOfDay.fromDateTime(local), alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context))}';
expect(find.text(expected), findsOneWidget);
final copy = tester.widget<ChatCopyButton>(find.byType(ChatCopyButton));
expect(copy.text, 'Answer');
expect(copy.text, isNot(contains('2026')));
});
testWidgets('omits absent and invalid server times', (tester) async {
_phone(tester);
final rows = transcriptRows([
{
'id': 'u1',
'role': 'user',
'created_at': 'not a timestamp',
'parts': [
{'type': 'text', 'text': 'Question'},
],
},
{
'id': 'a1',
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'Answer'},
],
},
]);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView(
children: [for (final row in rows) ChatMessageView(row: row)],
),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(ChatMessageTimestamp), findsNothing);
});
testWidgets('Material dark user row keeps exact text and attachments at 2x', (
tester,
) async {
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_link_text.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_timestamp.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_transcript_selection.dart';
@@ -30,6 +31,7 @@ void main() {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('User question'),
ChatMessageTimestamp(createdAt: 1792483920000),
SizedBox(height: 24),
ChatMarkdownView(text: 'Assistant answer.'),
],
@@ -49,6 +51,9 @@ void main() {
);
await copyTranscriptSelection(tester);
expect(copied, hasLength(1));
expect(copied.single, isNot(contains('2026')));
expect(copied.single, isNot(contains('AM')));
expect(copied.single, isNot(contains('PM')));
expect(copied.single, contains('User question'));
expect(copied.single, contains('Assistant answer.'));
expect(
@@ -121,6 +126,7 @@ void main() {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChatLinkText('User question'),
ChatMessageTimestamp(createdAt: 1792483920000),
ChatMarkdownView(text: 'Assistant answer.'),
],
),
@@ -166,6 +172,7 @@ void main() {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const ChatLinkText('User question'),
ChatMessageTimestamp(createdAt: 1792483920000),
const ChatMarkdownView(text: 'Assistant answer.'),
Text('Activity revision $revision'),
],