Files
pipelines/app/tool/search_selection_probe_entry.dart
T

470 lines
13 KiB
Dart

// Standalone fixture entry for real-browser Search result-selection evidence.
//
// Build and probe (from app/):
// flutter build web --target tool/search_selection_probe_entry.dart \
// --output build/probe-search-selection
// node tool/search_selection_probe.mjs --root build/probe-search-selection \
// --out build/search-selection-probe
//
// This is the normal application composition (`PipelinesApp`) over the shared
// transport, with the socket replaced by an in-process channel that answers
// the real `search`, `get_run`, `list_runs`, `list_pipelines`, `get_pipeline`
// and `ping` requests with server-shaped fixtures. Nothing here opens a
// network connection or touches a live server; the fixtures exist only so the
// shell's search → run-detail selection can be rendered and captured.
import 'dart:async';
import 'dart:convert';
import 'package:pipelines_app/application/app_url_strategy.dart';
import 'package:pipelines_app/application/pipelines_app.dart';
import 'package:pipelines_app/application/server_endpoint.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:flutter/widgets.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
/// The run every run-scoped hit names. Its recorded sequences are deliberately
/// not its row indexes (plan 5, build 2, plan 1), exactly like the unit
/// fixture, so a selection has to resolve the recorded sequence.
JsonMap probeRunDetail() => {
'id': 7,
'title': 'Release train run',
'name': 'release-train',
'status': 'succeeded',
'created_at': '2026-10-20T08:12:00Z',
'finished_at': '2026-10-20T08:42:00Z',
'workspace': '/srv/probe/release',
'input': {'prompt': 'ship it'},
'output': {'result': 'ok'},
'steps': [
{
'stage': 'plan',
'sequence': 5,
'status': 'completed',
'outcome': 'success',
'target': '#success',
'started_at': '2026-10-20T08:30:00Z',
'finished_at': '2026-10-20T08:31:00Z',
'diagnostic': 'release plan accepted',
},
{
'stage': 'build',
'sequence': 2,
'status': 'completed',
'outcome': 'success',
'target': 'plan',
'started_at': '2026-10-20T08:16:00Z',
'finished_at': '2026-10-20T08:24:00Z',
'artifacts': [
{
'name': 'release-report.txt',
'content_type': 'text/plain',
'size_bytes': 12,
'url': '/artifacts/7/2/release-report.txt',
},
],
},
{
'stage': 'plan',
'sequence': 1,
'status': 'completed',
'outcome': 'success',
'target': 'build',
'started_at': '2026-10-20T08:12:00Z',
'finished_at': '2026-10-20T08:14:00Z',
},
],
};
List<JsonMap> probeRuns() => [
{
'id': 7,
'title': 'Release train run',
'name': 'release-train',
'status': 'succeeded',
'created_at': '2026-10-20T08:12:00Z',
'finished_at': '2026-10-20T08:42:00Z',
'pipeline_id': 24,
},
{
'id': 6,
'title': 'Nightly docs run',
'name': 'nightly-docs',
'status': 'failed',
'created_at': '2026-10-19T22:00:00Z',
'finished_at': '2026-10-19T22:03:00Z',
'pipeline_id': 22,
},
];
List<JsonMap> probePipelines() => [
{
'id': 24,
'name': 'Release train',
'version': 5,
'stages': 2,
'created_at': '2026-09-28T10:12:00',
'updated_at': '2026-10-20T09:18:00',
'scheduled': false,
'schedule_count': 0,
'enabled_schedule_count': 0,
},
{
'id': 22,
'name': 'Nightly documentation',
'version': 6,
'stages': 1,
'created_at': '2026-10-02T18:20:00',
'updated_at': '2026-10-18T16:05:00',
'scheduled': true,
'schedule_count': 1,
'enabled_schedule_count': 1,
},
];
const _releaseSource = 'name: Release train\n'
'stages:\n'
' plan:\n'
' type: model\n'
' output: plan\n'
' build:\n'
' type: model\n'
' output: release\n';
JsonMap probePipelineDetail(int id, int requestedVersion) {
final summary = probePipelines().firstWhere(
(pipeline) => pipeline['id'] == id,
orElse: () => probePipelines().first,
);
final latest = summary['version'] as int;
final version = requestedVersion > 0 ? requestedVersion : latest;
return {
...summary,
'version': version,
'latest_version': latest,
'version_created_at': '2026-10-18T16:05:00',
'content_hash': 'sha256:probe-$id-v$version',
'source': _releaseSource,
'versions': [
for (var v = 1; v <= latest; v++)
{'version': v, 'stages': summary['stages'], 'created_at': '2026-10-0${v}T10:00:00'},
],
};
}
/// One recorded hit per result kind, each with the text a server would have
/// matched (`haystack`). The payloads are server-shaped `search_results` rows:
/// the `target` tokens are the ones `internal/protocol/search.go` records.
///
/// `release` matches every kind once; `retired` matches only the retired-field
/// hit; `missing` matches only an artifact whose recorded visit (sequence 9)
/// the run never recorded; `first` matches only the plan visit 1 step hit.
const List<({String haystack, JsonMap hit})> probeHits = [
(
haystack: 'Release train pipeline definition',
hit: {
'kind': 'pipeline',
'target': 'pipeline:24',
'score': 3,
'recency': '2026-10-20T09:18:00Z',
'pipeline': {'id': 24, 'name': 'Release train', 'version': 5, 'stages': 2},
},
),
(
haystack: 'name: Nightly documentation release notes',
hit: {
'kind': 'version',
'target': 'pipeline:22:v6',
'score': 2,
'recency': '2026-10-18T16:05:00Z',
'version': {
'pipeline_id': 22,
'version': 6,
'content_hash': '9f2c1ad',
'created_at': '2026-10-18T16:05:00Z',
},
},
),
(
haystack: 'Release train run',
hit: {
'kind': 'run',
'target': 'run:7',
'score': 2,
'recency': '2026-10-20T08:42:00Z',
'run': {
'id': 7,
'pipeline_name': 'Release train',
'status': 'succeeded',
'workspace': '/srv/probe/release',
'created_at': '2026-10-20T08:12:00Z',
'finished_at': '2026-10-20T08:42:00Z',
},
},
),
(
haystack: 'release plan accepted',
hit: {
'kind': 'step',
'target': 'run:7:5',
'score': 1,
'recency': '2026-10-20T08:31:00Z',
'step': {
'run_id': 7,
'sequence': 5,
'stage': 'plan',
'visit': 2,
'outcome': 'success',
'target': '#success',
'diagnostic': 'release plan accepted',
},
},
),
(
haystack: 'ship the release',
hit: {
'kind': 'field',
'target': 'run:7:5:output',
'score': 1,
'matched_field': 'output',
'recency': '2026-10-20T08:31:00Z',
'field': {
'run_id': 7,
'sequence': 5,
'field': 'output',
'snippet': 'ship the release',
},
},
),
(
haystack: 'release-report.txt',
hit: {
'kind': 'artifact',
'target': '/artifacts/7/2/release-report.txt',
'score': 1,
'recency': '2026-10-20T08:24:00Z',
'artifact': {
'run_id': 7,
'sequence': 2,
'name': 'release-report.txt',
'content_type': 'text/plain',
'size_bytes': 12,
'url': '/artifacts/7/2/release-report.txt',
},
},
),
(
haystack: 'retired evidence note',
hit: {
'kind': 'field',
'target': 'run:7:5:evidence',
'score': 1,
'matched_field': 'evidence',
'recency': '2026-10-20T08:31:00Z',
'field': {
'run_id': 7,
'sequence': 5,
'field': 'evidence',
'snippet': 'retired evidence note',
},
},
),
(
haystack: 'missing.log',
hit: {
'kind': 'artifact',
'target': '/artifacts/7/9/missing.log',
'score': 1,
'recency': '2026-10-20T08:40:00Z',
'artifact': {
'run_id': 7,
'sequence': 9,
'name': 'missing.log',
'content_type': 'text/plain',
'size_bytes': 4096,
'url': '/artifacts/7/9/missing.log',
},
},
),
(
haystack: 'first plan visit',
hit: {
'kind': 'step',
'target': 'run:7:1',
'score': 1,
'recency': '2026-10-20T08:14:00Z',
'step': {
'run_id': 7,
'sequence': 1,
'stage': 'plan',
'visit': 1,
'outcome': 'success',
'target': 'build',
},
},
),
];
/// Answers one decoded request envelope with the server-shaped reply, or null
/// for a send-only request the real server does not acknowledge.
JsonMap? probeReply(JsonMap request) {
final id = request['id'];
final type = '${request['type']}';
JsonMap error(String message) => {'id': id, 'type': 'error', 'error': message};
switch (type) {
case 'subscribe':
case 'unsubscribe':
return null;
case 'ping':
return {
'id': id,
'type': 'pong',
'build': {'version': 'Search selection probe fixture'},
'time': '2026-10-20T09:00:00Z',
};
case 'list_runs':
return {'id': id, 'type': 'runs', 'runs': probeRuns(), 'usage': null};
case 'get_run':
if (request['run_id'] != 7) {
return error('Fixture run ${request['run_id']} is unavailable');
}
return {'id': id, 'type': 'run', 'run_detail': probeRunDetail()};
case 'list_pipelines':
return {'id': id, 'type': 'pipelines', 'pipelines': probePipelines()};
case 'get_pipeline':
final pipelineId = request['pipeline_id'];
if (pipelineId is! int ||
!probePipelines().any((pipeline) => pipeline['id'] == pipelineId)) {
return error('Fixture pipeline $pipelineId is unavailable');
}
final version = request['version'];
return {
'id': id,
'type': 'pipeline',
'detail': probePipelineDetail(pipelineId, version is int ? version : 0),
};
case 'search':
// The app must send exactly the browser's SearchRequest body; anything
// else is answered with a visible refusal so the capture cannot pass.
final search = request['search'];
if (search is! Map ||
search.length != 3 ||
search['query'] is! String ||
search['kind'] is! String ||
search['limit'] != 0) {
return error('Fixture refused a malformed search request: $search');
}
final query = (search['query'] as String).trim().toLowerCase();
final kind = search['kind'] as String;
final results = [
for (final entry in probeHits)
if ((kind.isEmpty || entry.hit['kind'] == kind) &&
(query.isEmpty || entry.haystack.toLowerCase().contains(query)))
entry.hit,
];
return {
'id': id,
'type': 'search_results',
'search_results': {
'query': search['query'],
'kind': kind,
'results': results,
'truncated': false,
'scanned_steps': 3,
'scanned_bytes': 2048,
'cap': 50,
'scan_bound_bytes': 1048576,
},
};
default:
return error('Fixture does not implement $type');
}
}
/// An in-process websocket channel: every frame the client sends is decoded
/// and answered asynchronously with [probeReply], as the server would over the
/// wire. `ready` completes at once so the shared client reports connected.
class _FixtureChannel implements WebSocketChannel {
_FixtureChannel();
final _incoming = StreamController<dynamic>();
late final _FixtureSink _sink = _FixtureSink(this);
@override
Stream<dynamic> get stream => _incoming.stream;
@override
WebSocketSink get sink => _sink;
@override
Future<void> get ready => Future<void>.value();
@override
String? get protocol => null;
@override
int? get closeCode => null;
@override
String? get closeReason => null;
void receive(dynamic data) {
if (data is! String) return;
final decoded = jsonDecode(data);
if (decoded is! Map<String, dynamic>) return;
final reply = probeReply(decoded);
if (reply == null) return;
Future<void>.delayed(const Duration(milliseconds: 120), () {
if (!_incoming.isClosed) _incoming.add(jsonEncode(reply));
});
}
Future<void> close() async {
if (!_incoming.isClosed) await _incoming.close();
}
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _FixtureSink implements WebSocketSink {
_FixtureSink(this.channel);
final _FixtureChannel channel;
@override
void add(dynamic event) => channel.receive(event);
@override
void addError(Object error, [StackTrace? stackTrace]) => throw error;
@override
Future<void> addStream(Stream<dynamic> stream) async {
await for (final item in stream) {
add(item);
}
}
@override
Future<void> close([int? closeCode, String? closeReason]) => channel.close();
@override
Future<void> get done => channel._incoming.done;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
void main() {
final params = Uri.base.queryParameters;
final cupertino = params['platform'] == 'cupertino';
useAppUrlStrategy();
runApp(
PipelinesApp(
server: ServerEndpoint.resolve(base: Uri.base, web: true),
client: PipelinesClient(channelFactory: (_) => _FixtureChannel()),
cupertino: cupertino,
),
);
}