import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/servers/elevation/machine_elevation.dart'; import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart'; import 'package:pipelines_app/features/chat/files/files_upload.dart'; import '../../../core/servers/elevation/elevating_gateway_test.dart' show FakeMachine; void main() { late FilesUploadPost original; setUp(() => original = postFileUpload); tearDown(() => postFileUpload = original); final file = ChatPickedFile( name: 'notes.txt', mime: 'text/plain', bytes: Uint8List.fromList([1, 2, 3]), ); const refusal = ( status: 403, body: '{"code":"elevation_required","error":"Confirm your password."}', ); const stored = (status: 200, body: '{"path":"/home/demo/notes.txt"}'); test( 'a refused upload elevates its machine and retries with the token', () async { final machine = FakeMachine(); var prompts = 0; final elevation = MachineElevation( name: 'laptop', gateway: machine, prompt: (m) async { prompts++; return await m.submit('right') == null; }, ); final sent = >[]; postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) async { sent.add(headers); return headers['X-Pipelines-Elevation'] == 'tok-1' ? stored : refusal; }; final path = await uploadFileInto( 'ws://example.test/connect', '/home/demo', file, elevation: elevation, ); expect(path, '/home/demo/notes.txt'); expect(prompts, 1); expect(sent, [ {}, {'X-Pipelines-Elevation': 'tok-1'}, ]); // A live elevation rides on the next upload without asking again. await uploadFileInto( 'ws://example.test/connect', '/home/demo', file, elevation: elevation, ); expect(prompts, 1); expect(sent.last, {'X-Pipelines-Elevation': 'tok-1'}); }, ); test('a cancelled confirmation reports the refusal', () async { final machine = FakeMachine(); final elevation = MachineElevation( name: 'laptop', gateway: machine, prompt: (_) async => false, ); var posts = 0; postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) async { posts++; return refusal; }; await expectLater( uploadFileInto( 'ws://example.test/connect', '/home/demo', file, elevation: elevation, ), throwsA( isA().having( (e) => e.message, 'message', contains('confirm the password'), ), ), ); expect(posts, 1); }); test('refusal detection needs the typed code', () { expect(refusedForElevation(refusal), isTrue); expect( refusedForElevation((status: 403, body: 'folder access denied')), isFalse, ); expect(refusedForElevation((status: 200, body: refusal.body)), isFalse); }); }