Files
pipelines/app/test/features/templates/data/template_create_test.dart
T
alan cd9ec62d28 feat(app): add template catalogue and create-only submit repository
Add the nonvisual templates data/domain slice: TemplateRepository over the
shared ApiGateway for list_templates, validate and create-only submit, and
TemplateEntry preserving every server field in a deeply read-only raw map.

Re-include test/**/data/ in app/.gitignore so the mirroring tests are not
silently dropped from a fresh checkout.

Tests cover exact request shapes, source preservation, blank-name refusal,
malformed replies, server refusals and immutability.
2026-10-21 15:31:10 +01:00

148 lines
4.0 KiB
Dart

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 = <String, dynamic>{
'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 <JsonMap>[
<String, dynamic>{'type': 'validated'},
<String, dynamic>{'type': 'validated', 'graph': null},
]) {
final gateway = RecordingGateway([reply]);
await expectLater(
TemplateRepository(gateway).validate(source),
throwsA(
isA<TemplateRepositoryException>().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<TemplateRepositoryException>().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 = <String, dynamic>{
'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([
<String, dynamic>{'type': 'submitted'},
]);
await expectLater(
TemplateRepository(gateway).submit('Release', source),
throwsA(
isA<TemplateRepositoryException>().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']);
});
}