import 'package:halkyon_learn_flutter/src/core/content/rich_content.dart'; import 'package:halkyon_learn_flutter/src/feature/publish/data/publish_gateway.dart'; import 'package:halkyon_learn_flutter/src/feature/publish/model/publication.dart'; /// A [PublishGateway] in memory, on an instance whose account domain is /// `halkyon.eu`, for the person `raisondetredev`: each publish a release /// (unless [refusals] has one waiting), the name fixed from the first. class FakePublishGateway({ /// The studysets published, by id. final Map publications = const {}, }) implements PublishGateway { late final _publications = {...publications}; /// Refusals to answer the next publishes with, the first first. final refusals = []; /// What each publish was asked: the studyset, name and notes. final publishes = <({String studysetId, String slug, String notes})>[]; /// The description for Explore each publish sent, in order. final descriptions = []; /// When set, every request fails with it. Exception? fail; @override Future publication(String studysetId) async { if (fail case final error?) { throw error; } return _publications[studysetId]; } @override Future> published() async { if (fail case final error?) { throw error; } return { for (final MapEntry(:key, :value) in _publications.entries) if (!value.withdrawn) key, }; } @override Future withdraw(String studysetId) async { if (fail case final error?) { throw error; } final before = _publications[studysetId]!; return _publications[studysetId] = Publication( publicId: before.publicId, slug: before.slug, location: before.location, releases: before.releases, withdrawn: true, ); } @override Future publish( String studysetId, { required String slug, required String notes, RichContent? description, }) async { if (fail case final error?) { throw error; } publishes.add((studysetId: studysetId, slug: slug, notes: notes)); descriptions.add(description); if (refusals.isNotEmpty) { throw refusals.removeAt(0); } final before = _publications[studysetId]; if (before != null && before.slug != slug) { throw PublishRefused(PublishProblem.slugFixed); } return _publications[studysetId] = Publication( publicId: before?.publicId ?? 'public-$studysetId', // A release brings a withdrawn studyset back. slug: slug, location: 'halkyon.eu/@raisondetredev/$slug', releases: [ Release( version: (before?.latest?.version ?? 0) + 1, releasedAt: DateTime.utc(2026, 10, 9), license: 'CC-BY-SA-4.0', notes: notes, description: description, ), ...?before?.releases, ], ); } }