import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/templates/data/template_repository.dart'; import 'template_test_support.dart'; void main() { test( 'validate sends the exact source and returns the server graph', () async { final graph = { 'stages': [ {'key': 'build', 'type': 'model'}, ], 'unknown_graph_field': true, }; final gateway = RecordingGateway([ {'type': 'validated', 'graph': graph}, ]); final result = await TemplateRepository(gateway).validate(source); expect(gateway.calls.single.type, 'validate'); expect(gateway.calls.single.fields, {'source': source}); expect(gateway.calls.single.fields['source'], source); expect(result, graph); }, ); test( 'validate rejects a reply without a graph instead of faking success', () async { for (final reply in [ {'type': 'validated'}, {'type': 'validated', 'graph': null}, ]) { final gateway = RecordingGateway([reply]); await expectLater( TemplateRepository(gateway).validate(source), throwsA( isA().having( (error) => error.message, 'message', 'The server returned no graph.', ), ), ); } }, ); test('validate propagates a server refusal verbatim', () async { const refusal = ServerRefusal('invalid stage "aƱadir": unknown key'); final gateway = RefusingGateway(refusal); await expectLater( TemplateRepository(gateway).validate(source), throwsA(same(refusal)), ); expect(gateway.calls, ['validate']); }); test('submit refuses a blank name without sending anything', () async { for (final name in ['', ' ', '\t\n']) { final gateway = RecordingGateway([ { 'detail': {'id': 1}, }, ]); await expectLater( TemplateRepository(gateway).submit(name, source), throwsA( isA().having( (error) => error.message, 'message', 'Enter a pipeline name.', ), ), ); expect(gateway.calls, isEmpty); } }); test( 'submit trims the name, preserves the source and is always create-only', () async { final detail = { 'id': 9, 'name': 'Release 2', 'version': 1, }; final gateway = RecordingGateway([ {'type': 'submitted', 'detail': detail}, ]); final result = await TemplateRepository( gateway, ).submit(' Release 2 ', source); expect(gateway.calls.single.type, 'submit'); expect(gateway.calls.single.fields, { 'name': 'Release 2', 'source': source, 'create_only': true, }); expect(gateway.calls.single.fields.keys, [ 'name', 'source', 'create_only', ]); expect(result, detail); }, ); test( 'submit rejects a reply without a detail instead of faking success', () async { final gateway = RecordingGateway([ {'type': 'submitted'}, ]); await expectLater( TemplateRepository(gateway).submit('Release', source), throwsA( isA().having( (error) => error.message, 'message', 'The server returned no pipeline detail.', ), ), ); }, ); test('submit propagates a server refusal verbatim', () async { const refusal = ServerRefusal('a pipeline named "Release" already exists'); final gateway = RefusingGateway(refusal); await expectLater( TemplateRepository(gateway).submit(' Release ', source), throwsA(same(refusal)), ); expect(gateway.calls, ['submit']); }); }