merge(app): land checked templates and create-only request layer

This commit is contained in:
2026-10-21 15:35:43 +01:00
7 changed files with 649 additions and 2 deletions
@@ -0,0 +1,147 @@
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']);
});
}
@@ -0,0 +1,165 @@
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(
'list_templates sends only the type and preserves every field',
() async {
final catalogue = <JsonMap>[
{
'key': 'release',
'name': 'Release',
'description': 'Ship a version',
'source': source,
'scheduled': true,
'future_field': {
'nested': [1, 2, 3],
},
},
{
'key': 'blank',
'name': '',
'description': '',
'source': 'name: empty',
'scheduled': false,
},
];
final gateway = RecordingGateway([
{'templates': catalogue},
]);
final entries = await TemplateRepository(gateway).listTemplates();
expect(gateway.calls, hasLength(1));
expect(gateway.calls.single.type, 'list_templates');
expect(gateway.calls.single.fields, isEmpty);
expect(entries, hasLength(2));
expect(entries.first.key, 'release');
expect(entries.first.name, 'Release');
expect(entries.first.description, 'Ship a version');
expect(entries.first.source, source);
expect(entries.first.scheduled, isTrue);
expect(entries.first.raw['future_field'], {
'nested': [1, 2, 3],
});
expect(entries.last.key, 'blank');
expect(entries.last.scheduled, isFalse);
expect(entries.last.raw, isNot(contains('missing')));
},
);
test('list_templates leaves the server reply untouched', () async {
final first = <String, dynamic>{
'key': 'one',
'name': 'One',
'source': source,
'scheduled': true,
'unknown': ['keep', 'me'],
};
final reply = <String, dynamic>{
'templates': [first],
'usage': {'templates': 1},
};
final gateway = RecordingGateway([reply]);
final entries = await TemplateRepository(gateway).listTemplates();
expect(reply['templates'], hasLength(1));
expect(first, {
'key': 'one',
'name': 'One',
'source': source,
'scheduled': true,
'unknown': ['keep', 'me'],
});
expect(entries.single.raw['unknown'], ['keep', 'me']);
expect(
() => entries.single.raw['name'] = 'changed',
throwsUnsupportedError,
);
expect(first['name'], 'One');
});
test('list_templates freezes nested unknown data in raw', () async {
final nested = <String, dynamic>{
'nested': [1, 2, 3],
};
final gateway = RecordingGateway([
{
'templates': [
{
'key': 'one',
'name': 'One',
'source': source,
'scheduled': true,
'future_field': nested,
},
],
},
]);
final entries = await TemplateRepository(gateway).listTemplates();
final raw = entries.single.raw;
final future = raw['future_field'] as Map;
expect(() => future['added'] = true, throwsUnsupportedError);
expect(() => (future['nested'] as List).add(4), throwsUnsupportedError);
expect(future['nested'], [1, 2, 3]);
// The unmodifiable copies do not alias the server reply either.
nested['added'] = true;
expect(raw['future_field'], isNot(contains('added')));
});
test('list_templates rejects a catalogue that is not a list', () async {
final gateway = RecordingGateway([
<String, dynamic>{
'templates': {'key': 'not-a-list'},
},
]);
await expectLater(
TemplateRepository(gateway).listTemplates(),
throwsA(
isA<TemplateRepositoryException>().having(
(error) => error.message,
'message',
'The server returned no template catalogue.',
),
),
);
});
test(
'list_templates rejects a catalogue entry that is not an object',
() async {
final gateway = RecordingGateway([
<String, dynamic>{
'templates': ['not-an-entry'],
},
]);
await expectLater(
TemplateRepository(gateway).listTemplates(),
throwsA(
isA<TemplateRepositoryException>().having(
(error) => error.message,
'message',
'The server returned a template that is not an object.',
),
),
);
},
);
test('list_templates treats a reply with no catalogue as empty', () async {
final gateway = RecordingGateway([<String, dynamic>{}]);
final entries = await TemplateRepository(gateway).listTemplates();
expect(entries, isEmpty);
expect(gateway.calls.single.type, 'list_templates');
});
}
@@ -0,0 +1,47 @@
import 'package:pipelines_app/core/api_gateway.dart';
/// A recording gateway: it captures every request and answers from [replies].
class RecordingGateway implements ApiGateway {
RecordingGateway([List<JsonMap> replies = const []])
: _replies = [...replies];
final List<JsonMap> _replies;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add((type: type, fields: Map<String, dynamic>.from(fields)));
return _replies.removeAt(0);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
/// A gateway that refuses every request with the same server error object.
class RefusingGateway implements ApiGateway {
RefusingGateway(this.refusal);
final Object refusal;
final List<String> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add(type);
throw refusal;
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
class ServerRefusal implements Exception {
const ServerRefusal(this.message);
final String message;
@override
String toString() => message;
}
/// A source with Unicode, tabs, newlines and significant leading/trailing
/// whitespace: every request must carry it unchanged.
const source = ' name: "café"\n\tstages:\n - key: "añadir" 😀 \n';