Files
cutdex/test/diagnostics/app_diagnostics_test.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

190 lines
6.1 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:cutdex/diagnostics/app_diagnostics.dart';
import 'package:cutdex/diagnostics/diagnostic_store.dart';
import 'package:cutdex/diagnostics/request_report.dart';
import 'package:cutdex_sdk/runtime.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late Directory root;
late AppDiagnostics diagnostics;
setUp(() async {
root = await Directory.systemTemp.createTemp('cutdex-app-diagnostics-');
diagnostics = AppDiagnostics(
DiagnosticStore(Directory('${root.path}/logs')),
sessionId: 'session',
);
AppDiagnostics.instance = diagnostics;
});
tearDown(() async {
AppDiagnostics.instance = null;
await diagnostics.store.flush();
await root.delete(recursive: true);
});
Future<Map<String, dynamic>> exported() async => jsonDecode(
await (await diagnostics.export(Directory('${root.path}/out')))
.readAsString(),
) as Map<String, dynamic>;
test(
'SDK events and Agent reports share request and session identifiers',
() async {
const requestId = '12345678-1234-1234-1234-123456789abc';
await withDiagnosticContext({'requestId': requestId}, () async {
AppDiagnostics.failure(
'preview',
'start',
const RuntimeException(
code: 'failed',
message: 'decode',
domain: 'playback',
details: {'nativeCode': '-11841'},
),
StackTrace.fromString('ORIGINAL STACK'),
);
await diagnostics.request(
const RequestReport(
outcome: 'failed',
elapsedUs: 1,
frames: RequestFrameReport(
count: 0,
slowFrames: 0,
maxBuildUs: 0,
maxRasterUs: 0,
maxTimerDelayUs: 0,
),
runs: [],
),
requestId,
);
});
final report = await exported();
final event = (report['events'] as List).single;
expect(event['context']['requestId'], requestId);
expect(event['sessionId'], 'session');
expect(event['stackTrace'], 'ORIGINAL STACK');
expect(event['error']['nativeDetails']['nativeCode'], '-11841');
expect((report['requests'] as List).single['requestId'], requestId);
expect((report['requests'] as List).single['sessionId'], 'session');
expect((report['requests'] as List).single['at'], isNotNull);
},
);
test(
'SDK error severity uses reserved capacity without synchronous writes',
() async {
diagnostics = AppDiagnostics(
DiagnosticStore(
Directory('${root.path}/reserved'),
maxPending: 8,
errorReserveRecords: 2,
flushInterval: const Duration(days: 1),
),
);
AppDiagnostics.instance = diagnostics;
for (var i = 0; i < 8; i++) {
AppDiagnostics.report(
const DiagnosticEvent(
component: 'editor',
operation: 'split',
level: DiagnosticSeverity.info,
),
);
}
AppDiagnostics.failure(
'editor',
'setCaption',
StateError('failed'),
StackTrace.fromString('PROPERTY ERROR STACK'),
);
expect(File('${root.path}/reserved/fatal.jsonl').existsSync(), isFalse);
final events = (await exported())['events'] as List;
final error = events.singleWhere((e) => e['level'] == 'error');
expect(error['operation'], 'setCaption');
expect(error['stackTrace'], 'PROPERTY ERROR STACK');
expect(diagnostics.store.dropped, 2);
expect(diagnostics.store.droppedErrors, 0);
},
);
test(
'global handlers record errors and preserve existing handler behavior',
() async {
final flutter = FlutterError.onError;
final platform = PlatformDispatcher.instance.onError;
addTearDown(() {
FlutterError.onError = flutter;
PlatformDispatcher.instance.onError = platform;
});
var flutterCalls = 0, platformCalls = 0;
FlutterError.onError = (_) {
flutterCalls++;
};
PlatformDispatcher.instance.onError = (_, _) {
platformCalls++;
return true;
};
diagnostics.installErrorHandlers();
FlutterError.onError!(
FlutterErrorDetails(
exception: StateError('framework'),
stack: StackTrace.current,
),
);
expect(
PlatformDispatcher.instance.onError!(
StateError('async'),
StackTrace.current,
),
isTrue,
);
expect(File('${root.path}/logs/fatal.jsonl').existsSync(), isTrue);
final report = await exported();
expect(flutterCalls, 1);
expect(platformCalls, 1);
expect(report['events'], hasLength(2));
},
);
test('application metadata reads the installed host version', () async {
const channel = MethodChannel('cutdex_native/device_info');
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
expect(call.method, 'getApplicationInfo');
return {'appVersion': '2.0.0', 'appBuild': '42'};
});
addTearDown(
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null),
);
await diagnostics.readApplicationInfo();
expect((await exported())['metadata']['appVersion'], '2.0.0');
expect(diagnostics.metadata['appBuild'], '42');
});
test(
'format and process failures do not serialize source or arguments',
() async {
AppDiagnostics.failure(
'test',
'parse',
const FormatException('Invalid JSON', 'PRIVATE BODY'),
StackTrace.current,
);
AppDiagnostics.failure(
'test',
'spawn',
const ProcessException('/worker', ['PRIVATE PROMPT'], 'Failed', 1),
StackTrace.current,
);
final encoded = jsonEncode(await exported());
expect(encoded, isNot(contains('PRIVATE')));
},
);
}