Files
pipelines/app/test/application/open_in_chat_test.dart
T
alan 5425001d82 Send artifacts, runs and pipelines to a new or existing chat
One picker offers a new chat (the saved Open in chat harness and model)
or any open chat. An existing chat gets the reference appended to its
draft and focused, never sent. Artifacts gain the action in their viewer
and gallery; the run and pipeline item becomes Send to chat.
2026-11-13 18:43:14 +00:00

508 lines
17 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/application/open_in_chat.dart';
import 'package:pipelines_app/application/send_to_chat.dart';
import 'package:pipelines_app/application/open_in_chat_preferences.dart';
import 'package:pipelines_app/features/chat/chat_runtime.dart';
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_chat_context.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_create_fields.dart';
import 'package:pipelines_app/features/pipelines/pipeline_models.dart';
import 'package:pipelines_app/features/pipelines/pipeline_repository.dart';
import 'package:pipelines_app/features/pipelines/pipelines_controller.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_archive_action.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_list_panel.dart';
import 'package:pipelines_app/features/runs/ui/run_list_row.dart';
import 'package:pipelines_app/features/runs/ui/run_more_menu.dart';
import '../features/chat/ui/sessions/fake_chat_gateway.dart';
import '../features/pipelines/fixtures.dart';
import '../features/pipelines/test_helpers.dart';
void main() {
setUp(() => SharedPreferences.setMockInitialValues({}));
testWidgets('the harness and model chosen in Settings start the chat', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
final gateway = FakeChatGateway()
..backendsHandler = () async => const {
'chat_backends': ['opencode', 'claude'],
};
await const OpenInChatPreferences(
harness: 'claude',
provider: 'anthropic',
model: 'claude-opus-5-5',
).save(gateway);
final runtime = ChatRuntime(gateway: gateway);
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
await openReferenceInChat(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (_) {},
directory: '/work/run',
title: 'Run #42: Build',
draft: 'About Pipelines run #42.',
);
final create = gateway.lastFields('chat_create')!['chat_create'] as Map;
expect(create['backend'], 'claude');
final model = gateway.lastFields('chat_model')!['chat_model'] as Map;
expect(model['session_id'], 'created');
expect(model['provider'], 'anthropic');
expect(model['model'], 'claude-opus-5-5');
// Opened for work: beside the active chat, with the keyboard in it.
expect(runtime.openedForWork, contains('created'));
runtime.dispose();
});
testWidgets('Send to chat lists and searches existing chats', (tester) async {
final gateway = FakeChatGateway()
..sessionsReply = {
'chat_sessions': [
{
'id': 'older',
'title': 'Old notes',
'directory': '/archive',
'backend': 'codex',
'updated_at': '2026-10-01T00:00:00Z',
},
{
'id': 'release',
'title': 'Release review',
'directory': '/work',
'backend': 'claude',
'updated_at': '2026-10-20T00:00:00Z',
},
],
};
final runtime = ChatRuntime(gateway: gateway);
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
final picker = showSendToChatPicker(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (_) {},
directory: '/work',
title: 'Artifact',
draft: 'artifact',
);
await tester.pumpAndSettle();
expect(find.text('New chat'), findsOneWidget);
expect(find.text('Release review'), findsOneWidget);
expect(find.text('Old notes'), findsOneWidget);
// A short list is only as tall as its rows, with no empty space below.
final rows = tester
.widgetList<ListTile>(find.byType(ListTile))
.map((tile) => tester.getSize(find.byWidget(tile)).height)
.fold<double>(0, (sum, height) => sum + height);
expect(tester.getSize(find.byType(ListView)).height, closeTo(rows, 1));
await tester.enterText(find.byType(TextField), 'release');
await tester.pump();
expect(find.text('Release review'), findsOneWidget);
expect(find.text('Old notes'), findsNothing);
await tester.tap(find.text('New chat'));
await picker;
runtime.dispose();
});
testWidgets('Send to chat appends an existing draft and focuses it', (
tester,
) async {
final gateway = FakeChatGateway()
..sessionsReply = {
'chat_sessions': [
{
'id': 'existing',
'title': 'Existing chat',
'directory': '/work',
'backend': 'opencode',
'updated_at': '2026-10-20T00:00:00Z',
},
],
};
final runtime = ChatRuntime(gateway: gateway);
final composer = ChatComposerController(gateway, 'existing');
await composer.setDraft('Before');
runtime.registerComposer('existing', composer);
var selected = '';
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
final picker = showSendToChatPicker(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (id) => selected = id,
directory: '/work',
title: 'Artifact',
draft: 'Artifact details',
);
await tester.pumpAndSettle();
await tester.tap(find.text('Existing chat'));
await picker;
await tester.pump();
expect(selected, 'existing');
expect(composer.draft, 'Before\nArtifact details');
expect(runtime.composerFocus.take('existing'), isTrue);
expect(gateway.calls.where((call) => call.type == 'chat_send'), isEmpty);
composer.dispose();
runtime.dispose();
});
testWidgets('a harness the server no longer offers falls back to the '
'default, without the saved model', (tester) async {
SharedPreferences.setMockInitialValues({});
final gateway = FakeChatGateway()
..backendsHandler = () async => const {
'chat_backends': ['opencode', 'codex'],
};
await const OpenInChatPreferences(
harness: 'claude',
provider: 'anthropic',
model: 'claude-opus-5-5',
).save(gateway);
final runtime = ChatRuntime(gateway: gateway);
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
await openReferenceInChat(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (_) {},
directory: '/work/run',
title: 'Run #42: Build',
draft: 'About Pipelines run #42.',
);
final create = gateway.lastFields('chat_create')!['chat_create'] as Map;
// The default harness (OpenCode) is the request's default: not claude.
expect(create['backend'], anyOf(isNull, 'opencode'));
expect(gateway.lastFields('chat_model'), isNull);
runtime.dispose();
});
test('a run summary names its pipeline in name', () {
final draft = runChatReference({
'id': 7,
'name': 'release',
'title': 'Ship notes',
'version': 3,
'status': 'failed',
});
expect(
draft,
'About Pipelines run #7 "Ship notes" (pipeline "release" v3, '
'status failed). Inspect it with `pipelines inspect 7`.',
);
final untitled = runChatReference({
'id': 8,
'name': 'release',
'version': 3,
'status': 'succeeded',
});
expect(untitled, contains('run #8 "Run 8" (pipeline "release" v3'));
final unversioned = runChatReference({
'id': 9,
'name': 'release',
'status': 'running',
});
expect(unversioned, contains('(pipeline "release", status running)'));
});
test('run references contain the exact inspect command', () {
expect(
runChatReference({
'id': 42,
'title': 'Nightly build',
'pipeline_name': 'Deploy',
'pipeline_version': 3,
'status': 'failed',
}),
'About Pipelines run #42 "Nightly build" (pipeline "Deploy" v3, '
'status failed). Inspect it with `pipelines inspect 42`.',
);
});
test('pipeline references contain show and edit guidance', () {
expect(
pipelineChatReference(7, 'Deploy', 4),
'About pipeline "Deploy" (id 7, version 4). Show it with '
'`pipelines show "Deploy"`; to change it, draft with `pipelines edit` '
'and publish with `pipelines submit`.',
);
});
test('artifact references identify provenance and inspection', () {
final reference = artifactChatReference(
const ArtifactChatContext(
runId: '42',
runTitle: 'Build',
pipelineName: 'Release',
stageKey: 'test',
visit: 2,
configuredEndpoint: 'ws://server.example/connect',
),
const ArtifactGroup(
stageKey: 'test',
visit: 2,
artifacts: [
ArtifactRecord(
name: 'report.txt',
contentType: 'text/plain',
sizeBytes: 12,
url: '/artifacts/42/2/report.txt',
raw: {},
),
],
),
const ArtifactRecord(
name: 'report.txt',
contentType: 'text/plain',
sizeBytes: 12,
url: '/artifacts/42/2/report.txt',
raw: {},
),
);
expect(reference, contains('report.txt'));
expect(reference, contains('pipelines inspect 42'));
expect(
reference,
contains('http://server.example/artifacts/42/2/report.txt'),
);
});
test('open in chat keeps the more menu available', () {
expect(showRunMoreMenu({'id': 42}, canOpenInChat: true), isTrue);
});
test('pipeline latest run retains its workspace', () {
final run = PipelineRun.fromJson({
'id': 9,
'pipeline_id': 7,
'workspace': '/work/deploy',
});
expect(run.workspace, '/work/deploy');
});
testWidgets(
'open in chat uses the catalog backend, draft, route, and focus',
(tester) async {
final gateway = FakeChatGateway()
..backendsHandler = () async => const {
'chat_backends': ['codex', 'claude'],
};
final runtime = ChatRuntime(gateway: gateway);
String? selected;
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
await openReferenceInChat(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (id) => selected = id,
directory: '/work/run',
title: 'Run #42: Build',
draft: 'About Pipelines run #42.',
);
final create = gateway.lastFields('chat_create')!['chat_create'] as Map;
expect(create['backend'], 'codex');
expect(create['directory'], '/work/run');
expect(selected, 'created');
expect(runtime.pendingDrafts['created'], 'About Pipelines run #42.');
expect(runtime.composerFocus.take('created'), isTrue);
runtime.dispose();
},
);
testWidgets('open in chat falls back to the default folder', (tester) async {
final gateway = FakeChatGateway();
final runtime = ChatRuntime(gateway: gateway);
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
await openReferenceInChat(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (_) {},
directory: '',
title: 'Pipeline: Deploy',
draft: 'About pipeline "Deploy".',
);
final create = gateway.lastFields('chat_create')!['chat_create'] as Map;
expect(create['directory'], chatHomeDirectory);
runtime.dispose();
});
testWidgets('create failure leaves navigation and chat state unchanged', (
tester,
) async {
final gateway = FakeChatGateway()..failures['chat_create'] = 'refused';
final runtime = ChatRuntime(gateway: gateway);
String? selected;
await tester.pumpWidget(const MaterialApp(home: Scaffold()));
await openReferenceInChat(
context: tester.element(find.byType(Scaffold)),
runtime: runtime,
selectChat: (id) => selected = id,
directory: '/work/run',
title: 'Run #42: Build',
draft: 'About Pipelines run #42.',
);
await tester.pump();
expect(selected, isNull);
expect(runtime.opened, isEmpty);
expect(find.textContaining('Could not open in chat'), findsOneWidget);
runtime.dispose();
});
testWidgets('run list More selects Send to chat on desktop', (tester) async {
final run = <String, dynamic>{
'id': 42,
'title': 'Build',
'status': 'failed',
'created_at': '2026-10-17T19:00:00',
};
JsonMap? selected;
final gateway = FakeChatGateway();
tester.view.physicalSize = const Size(900, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RunListRow(
run: run,
now: DateTime(2026, 10, 18),
gateway: gateway,
onRunChanged: (_) {},
onOpenInChat: (context, value) async => selected = value,
),
),
),
);
await tester.tap(find.byKey(const Key('run-list-more-menu-42')));
await tester.pumpAndSettle();
expect(find.text('Send to chat…'), findsOneWidget);
await tester.tap(find.text('Send to chat…'));
expect(selected, same(run));
});
testWidgets('run detail More selects Send to chat from the phone sheet', (
tester,
) async {
final run = <String, dynamic>{'id': 42, 'title': 'Build'};
JsonMap? selected;
final gateway = FakeChatGateway();
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
appBar: AppBar(
actions: [
RunMoreMenu(
run: run,
gateway: gateway,
onRunChanged: (_) {},
onOpenInChat: (context, value) async => selected = value,
),
],
),
),
),
);
await tester.tap(find.byTooltip('More actions'));
await tester.pumpAndSettle();
expect(find.text('Send to chat…'), findsOneWidget);
await tester.tap(find.text('Send to chat…'));
expect(selected, same(run));
});
testWidgets('pipeline list row More selects Send to chat with latest run', (
tester,
) async {
final fixture = PipelineFixture();
final gateway = gatewayFor(fixture);
final controller = PipelinesController(PipelinesRepository(gateway));
addTearDown(controller.dispose);
await controller.load();
PipelineSummary? selected;
PipelineRun? selectedRun;
tester.view.physicalSize = const Size(900, 700);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: PipelineListPanel(
controller: controller,
onOpenInChat: (context, pipeline, run) async {
selected = pipeline;
selectedRun = run;
},
),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('pipeline-row-more-menu-1')));
await tester.pumpAndSettle();
expect(find.text('Send to chat…'), findsOneWidget);
await tester.tap(find.text('Send to chat…'));
expect(selected?.id, 1);
expect(selectedRun?.id, 42);
});
testWidgets(
'pipeline detail actions select Send to chat from phone overflow',
(tester) async {
final fixture = PipelineFixture();
final gateway = gatewayFor(fixture);
final controller = PipelinesController(PipelinesRepository(gateway));
addTearDown(controller.dispose);
final detail = PipelineDetail.fromJson(fixture.details.first);
PipelineDetail? selected;
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => pipelineActionsMenu(
context,
controller,
detail,
onOpenInChat: (context, value, run) async => selected = value,
),
),
),
),
);
await tester.tap(find.byTooltip('Pipeline actions'));
await tester.pumpAndSettle();
expect(find.text('Send to chat…'), findsOneWidget);
await tester.tap(find.text('Send to chat…'));
expect(selected?.id, detail.id);
},
);
}