import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/search/domain/search_target.dart'; JsonMap pipelineHit([JsonMap overrides = const {}]) => { 'kind': 'pipeline', 'target': 'pipeline:7', 'pipeline': { 'id': 7, 'name': 'release', 'version': 3, 'stages': 4, ...overrides, }, }; JsonMap versionHit([JsonMap overrides = const {}]) => { 'kind': 'version', 'target': 'pipeline:9:v2', 'version': { 'pipeline_id': 9, 'version': 2, 'content_hash': 'abc', 'created_at': '2026-01-01T00:00:00Z', ...overrides, }, }; JsonMap runHit([JsonMap overrides = const {}]) => { 'kind': 'run', 'target': 'run:11', 'run': { 'id': 11, 'pipeline_name': 'release', 'status': 'succeeded', ...overrides, }, }; JsonMap stepHit([JsonMap overrides = const {}]) => { 'kind': 'step', 'target': 'run:11:4', 'step': { 'run_id': 11, 'sequence': 4, 'stage': 'builder', 'visit': 2, 'outcome': 'success', ...overrides, }, }; JsonMap fieldHit([JsonMap overrides = const {}]) => { 'kind': 'field', 'target': 'run:11:4:output', 'matched_field': 'output', 'field': { 'run_id': 11, 'sequence': 4, 'field': 'output', 'snippet': 'the release is ready', ...overrides, }, }; JsonMap artifactHit([JsonMap overrides = const {}]) => { 'kind': 'artifact', 'target': '/artifacts/11/4/notes.md', 'artifact': { 'run_id': 11, 'sequence': 4, 'name': 'notes.md', 'content_type': 'text/markdown', 'size_bytes': 2048, 'url': '/artifacts/11/4/notes.md', ...overrides, }, }; SearchTarget resolved(JsonMap hit) { final resolution = searchTargetFor(hit); expect(resolution, isA(), reason: '$hit'); return (resolution as ResolvedSearchTarget).target; } UnresolvedSearchTarget unresolved(JsonMap hit) { final resolution = searchTargetFor(hit); expect(resolution, isA(), reason: '$hit'); return resolution as UnresolvedSearchTarget; } void main() { test('every recorded hit kind serialises back to its server token', () { final pipeline = resolved(pipelineHit()); expect(pipeline, isA()); expect((pipeline as SearchPipelineTarget).pipelineId, 7); expect(pipeline.kind, SearchTargetKind.pipeline); expect(pipeline.wire, pipelineHit()['target']); final version = resolved(versionHit()); expect(version, isA()); expect((version as SearchVersionTarget).pipelineId, 9); expect(version.version, 2); expect(version.wire, versionHit()['target']); final run = resolved(runHit()); expect(run, isA()); expect((run as SearchRunTarget).runId, 11); expect(run.wire, runHit()['target']); final step = resolved(stepHit()); expect(step, isA()); expect((step as SearchStepTarget).runId, 11); expect(step.sequence, 4); expect(step.stage, 'builder'); expect(step.visit, 2); expect(step.wire, stepHit()['target']); final field = resolved(fieldHit()); expect(field, isA()); expect((field as SearchFieldTarget).runId, 11); expect(field.sequence, 4); expect(field.field, 'output'); expect(field.wire, fieldHit()['target']); final artifact = resolved(artifactHit()); expect(artifact, isA()); expect((artifact as SearchArtifactTarget).url, '/artifacts/11/4/notes.md'); expect(artifact.runId, 11); expect(artifact.sequence, 4); expect(artifact.wire, artifactHit()['target']); }); test('a payload without its identity is reported as unresolvable', () { expect(unresolved({}).reason, 'no recorded target payload'); expect(unresolved(pipelineHit({'id': null})).reason, contains('pipeline')); expect( unresolved(versionHit({'pipeline_id': 9, 'version': 0})).reason, contains('version'), ); expect(unresolved(runHit({'id': 0})).reason, contains('run')); expect( unresolved(stepHit({'run_id': 11, 'sequence': 0})).reason, contains('step'), ); expect( unresolved(fieldHit({'run_id': 11, 'sequence': 4, 'field': ' '})).reason, contains('field'), ); expect(unresolved(artifactHit({'url': ' '})).reason, contains('artifact')); expect( unresolved({'kind': 'future_kind', 'target': 'widget:7'}).reason, 'no recorded target payload', ); }); test('a hit without a matching recorded token is unresolvable', () { expect( unresolved({ 'kind': 'run', 'run': {'id': 11}, }).reason, 'recorded target is missing', ); expect( unresolved({...pipelineHit(), 'target': 'pipeline:8'}).reason, contains('does not match'), ); expect( unresolved({...stepHit(), 'target': 'run:11:5'}).reason, contains('does not match'), ); }); test('a serialised target parses back to the same identity', () { final targets = [ resolved(pipelineHit()), resolved(versionHit()), resolved(runHit()), resolved(stepHit()), resolved(fieldHit()), resolved(artifactHit()), ]; for (final target in targets) { final parsed = SearchTarget.fromWire(target.wire); expect(parsed, target, reason: target.wire); expect(parsed!.wire, target.wire, reason: target.wire); } // The token names the visit's run and sequence, not its stage/visit facts. final step = resolved(stepHit()) as SearchStepTarget; final parsed = SearchTarget.fromWire(step.wire) as SearchStepTarget; expect(parsed, step); expect(parsed.stage, isNull); expect(parsed.visit, isNull); }); test('a token outside the server vocabulary names no target', () { for (final token in const [ '', ' ', 'widget:7', 'pipeline:0', 'pipeline:7:v0', 'run:11:0', 'run:11:4:', 'http://example.test/not-an-artifact', ]) { expect(SearchTarget.fromWire(token), isNull, reason: token); } expect(SearchTarget.fromWire('run:11'), const SearchRunTarget(11)); expect( SearchTarget.fromWire('pipeline:7:v2'), const SearchVersionTarget(7, 2), ); expect(SearchTarget.fromWire('pipeline:7'), const SearchPipelineTarget(7)); }); }