import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart'; import 'package:pipelines_app/features/artifacts/domain/gallery_navigation.dart'; import 'artifact_fixtures.dart'; ArtifactRecord record(String name, {String contentType = '', String? url}) => ArtifactRecord.fromJson(artifact(name, contentType: contentType, url: url)); void main() { group('filtered gallery order', () { test('pages viewer artifacts group by group, in recorded order', () { final groups = [ ArtifactGroup( stageKey: 'reviewer', visit: 1, artifacts: [ record('session.html', contentType: 'text/html'), record('input.md', contentType: 'text/markdown'), ], ), ArtifactGroup( stageKey: 'implementer', visit: 2, artifacts: [ record('run.log', contentType: 'text/plain'), record('shot.png', contentType: 'image/png'), record('main.py', contentType: 'text/x-python'), record('clip.mp4', contentType: 'video/mp4'), record('memory.txt', contentType: 'text/plain'), record('rows.csv', contentType: 'text/csv'), record('output.md', contentType: 'application/octet-stream'), ], ), ]; expect(pagedArtifacts(groups).map((page) => page.artifact.name), [ 'input.md', 'run.log', 'shot.png', 'main.py', 'clip.mp4', 'memory.txt', 'rows.csv', 'output.md', ]); expect(pagedArtifacts(const []), isEmpty); }); test('a page keeps the stage visit it belongs to', () { final groups = artifactGroups(>[ step( stage: 'a', sequence: 1, visit: 3, artifacts: >[ artifact('a.png', contentType: 'image/png'), ], ), step( stage: 'b', sequence: 2, visit: 5, artifacts: >[ artifact('b.png', contentType: 'image/png'), ], ), ]); final pages = pagedArtifacts(groups); expect(pages.map((page) => '${page.stageKey}/${page.visit}'), [ 'a/3', 'b/5', ]); expect(pages.map((page) => page.pageIndex), [0, 1]); }); }); group('identity', () { test('duplicate names with different urls get distinct viewer indices', () { final groups = artifactGroups(>[ step( stage: 'a', sequence: 1, visit: 1, artifacts: >[ artifact( 'shot.png', url: '/artifacts/1/1/shot.png', contentType: 'image/png', ), ], ), step( stage: 'a', sequence: 2, visit: 2, artifacts: >[ artifact( 'shot.png', url: '/artifacts/1/2/shot.png', contentType: 'image/png', ), ], ), ]); final index = viewerIndexByUrl(groups); expect(index, { '/artifacts/1/1/shot.png': 0, '/artifacts/1/2/shot.png': 1, }); expect(pagedArtifacts(groups).map((page) => page.identity), [ '/artifacts/1/1/shot.png', '/artifacts/1/2/shot.png', ]); }); test('a repeated url maps to its first occurrence only', () { final groups = artifactGroups(>[ step( stage: 'a', sequence: 1, artifacts: >[ artifact( 'a.png', url: '/artifacts/1/1/a.png', contentType: 'image/png', ), artifact( 'a.png', url: '/artifacts/1/1/a.png', contentType: 'image/png', ), ], ), ]); final index = viewerIndexByUrl(groups); expect(index, {'/artifacts/1/1/a.png': 0}); expect(pagedArtifacts(groups).length, 2); }); test('artifacts without a url keep distinct fallback identities', () { final groups = artifactGroups(>[ step( stage: 'a', sequence: 1, visit: 2, artifacts: >[ artifact('shot.png', url: '', contentType: 'image/png'), artifact('shot.png', url: '', contentType: 'image/png'), ], ), ]); final pages = pagedArtifacts(groups); expect(pages.map((page) => page.identity).toSet().length, 2); expect(viewerIndexByUrl(groups), isEmpty); }); test('non-viewer artifacts never get a viewer identity', () { final groups = artifactGroups(>[ step( stage: 'a', sequence: 1, artifacts: >[ artifact('session.html', contentType: 'text/html'), artifact('bundle.zip', contentType: 'application/zip'), artifact('report.pdf', contentType: 'application/pdf'), ], ), ]); expect(pagedArtifacts(groups), isEmpty); expect(viewerIndexByUrl(groups), isEmpty); }); }); group('paging', () { test('previous and next clamp at both ends without wrapping', () { expect(pagedIndex(0, -1, 3), 0); expect(pagedIndex(1, -1, 3), 0); expect(pagedIndex(1, 1, 3), 2); expect(pagedIndex(2, 1, 3), 2); expect(pagedIndex(0, 0, 0), 0); expect(pagedIndex(0, 0, 1), 0); expect(pagedIndex(9, -3, 0), 0); }); test('the position label trims the content type and falls back', () { expect( artifactViewerPositionLabel(0, 3, 'image/png'), '1 of 3 · image/png', ); expect( artifactViewerPositionLabel(1, 3, 'image/png; charset=binary'), '2 of 3 · image/png', ); expect(artifactViewerPositionLabel(4, 5, ''), '5 of 5 · artifact'); expect(artifactViewerPositionLabel(0, 1, ' '), '1 of 1 · artifact'); }); }); }