runGlobalActionAttempt function
Future<void>
runGlobalActionAttempt(
- WidgetTester tester,
- EnsembleTestContext attempt,
- EnsembleConfig config
Runs one named global action before the compiled assertion steps. A keyed leaf supplies the same live BuildContext to script invokables and API calls even after the inherited scope is inserted above it.
Implementation
Future<void> runGlobalActionAttempt(
WidgetTester tester,
EnsembleTestContext attempt,
EnsembleConfig config,
) async {
final action = attempt.testCase.action!;
final configured = config.getResources()?[ResourceArtifactEntry.Actions.name];
final actions = ActionScopeUtil.normalizeActionsMap(
configured is Map ? configured : const {},
);
if (!actions.containsKey(action.name)) {
throw EnsembleTestFailure(
'Test "${attempt.testCase.id}" could not find global action "${action.name}".');
}
final key = GlobalKey();
final leaf = SizedBox.shrink(key: key);
await tester.pumpWidget(leaf);
final context = key.currentContext!;
final scope = ScopeManager(
DataContext(buildContext: context),
PageData(actionsMap: actions),
);
final runtime = GlobalActionRuntime();
attempt.globalActionRuntime = runtime;
try {
await tester.pumpWidget(
APIProviders(
providers: {
// APIProviders otherwise creates live fallback providers for these
// built-in types even when no such provider was configured.
'firebaseFunction': attempt.apiOverlay,
'sse': attempt.apiOverlay,
...?config.apiProviders,
'http': attempt.apiOverlay,
},
child: DataScopeWidget(scopeManager: scope, child: leaf),
),
);
var finished = false;
Object? error;
StackTrace? stack;
runtime
.run(
context: context,
scope: scope,
name: action.name,
inputs: action.inputs,
mocks: attempt.testCase.mocks.actions,
expectError: action.expectError,
)
.then<void>((_) => finished = true, onError: (Object e, StackTrace s) {
error = e;
stack = s;
finished = true;
});
// Flutter widget tests use a fake clock. Awaiting the Future directly
// would stall any delayed API fixture or callback scheduled by the action.
await EventDrivenSettler(tester: tester).waitFor(
condition: () => finished,
timeout: const TestExecutionConfig().defaultWaitTimeout,
);
final missingMock = attempt.apiOverlay.missingMock;
if (missingMock != null) throw EnsembleTestFailure(missingMock);
if (error != null) Error.throwWithStackTrace(error!, stack!);
} finally {
runtime.close();
attempt.logger.log(jsonEncode({
'globalAction': action.name,
'calls': runtime.calls,
'events': runtime.events,
}, toEncodable: (value) => value.toString()));
scope.dispose();
await tester.pumpWidget(const SizedBox.shrink());
}
}