import 'package:flutter_test/flutter_test.dart';
import 'package:tasq/utils/supabase_response.dart';

class _FakePostgrestLike {
  final dynamic error;
  final int? status;
  final String? statusText;
  _FakePostgrestLike({this.error, this.status, this.statusText});
}

void main() {
  test('extractSupabaseError returns null for null/ok responses', () {
    expect(extractSupabaseError(null), isNull);
    expect(extractSupabaseError({'data': []}), isNull);
  });

  test('extractSupabaseError extracts message from Map-shaped error', () {
    final res = {
      'error': {'message': 'boom'},
    };
    expect(extractSupabaseError(res), 'boom');

    final res2 = {'error': 'simple-error'};
    expect(extractSupabaseError(res2), 'simple-error');
  });

  test('extractSupabaseError extracts from Postgrest-like response', () {
    final r1 = _FakePostgrestLike(error: {'message': 'bad'});
    expect(extractSupabaseError(r1), 'bad');

    final r2 = _FakePostgrestLike(status: 500, statusText: 'server error');
    expect(extractSupabaseError(r2), 'server error');
  });
}
