Ark Navigation Test

Test real Ark Navigation configurations: Guard decisions, exact stacks, retained tabs, typed results and Entry lifecycle.

Русский

The package is intended as a dev_dependency; application runtime continues to use ark_navigation without depending on this companion.

Start with one complete test

From this package directory:

flutter pub get
flutter test example/basic_lifecycle_test.dart

The test opens two entries of one Route. Opening the second entry completes its committed Future. A later pop completes its completed Future with a String result. The first root entry remains: the router does not pop the final root page.

The entire tested file is below. The Graph and every helper class are included; no generated code, DI or omitted application service is required.

import 'package:ark_navigation/ark_navigation.dart';
import 'package:ark_navigation_test/ark_navigation_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test(
    'commit opens an entry; pop later completes it with a typed result',
    () async {
      final NavigationTestDriver driver = NavigationTestDriver(
        graph: _Fixture.graph(),
        sessionId: const NavigationSessionId('example'),
      );
      addTearDown(driver.close);
      final NavigationController navigation = driver.controller;
      expect(
        await NavigationTestWait.committed(
          const _Page(1).on(navigation).push(),
          timeout: const Duration(seconds: 1),
        ),
        isA<NavigationCommitted>(),
      );

      final NavigationTicket<String> ticket = const _Page(
        2,
      ).on(navigation).push();
      final NavigationOperationResult opened =
          await NavigationTestWait.committed(
            ticket,
            timeout: const Duration(seconds: 1),
          );
      expect(opened, isA<NavigationCommitted>());
      final NavigationEntryId id = (opened as NavigationCommitted).entryId!;

      expect(
        await NavigationTestWait.operation(
          navigation.pop(result: 'selected'),
          timeout: const Duration(seconds: 1),
        ),
        isA<NavigationCommitted>(),
      );
      final NavigationCompletion<String> closed =
          await NavigationTestWait.completed(
            ticket,
            timeout: const Duration(seconds: 1),
          );
      expect(closed, isA<NavigationPopped<String>>());
      expect((closed as NavigationPopped<String>).result, 'selected');
      expect(
        driver.observer,
        NavigationTestMatchers.hasLifecycleExit(
          sessionId: const NavigationSessionId('example'),
          entryId: id,
          reason: NavigationEntryExitReason.popped,
          count: 1,
        ),
      );
      expect(driver.observer, NavigationTestMatchers.hasNoObservedFailures());
    },
  );
}

final class _Page extends NavigationDestination<String> {
  const _Page(this.number);
  final int number;
}

abstract final class _Fixture {
  static NavigationGraph graph() => NavigationGraph(
    rootScopeId: const NavigationScopeId('root'),
    scopes: const <NavigationScopeDefinition>[
      NavigationScopeDefinition(id: NavigationScopeId('root')),
    ],
    modules: <NavigationModule>[
      NavigationModule(
        id: const NavigationModuleId('app'),
        routes: <NavigationRouteDefinitionBase>[
          NavigationRouteDefinition<_Page, String>(
            id: const NavigationRouteId('page'),
            restorationPolicy: NavigationRestorationPolicy.sessionOnly,
            pageBuilder:
                (
                  final BuildContext context,
                  final _Page destination,
                  final NavigationEntrySnapshot entry,
                ) => const SizedBox(),
          ),
        ],
      ),
    ],
    mounts: <NavigationMount>[
      NavigationMount(
        id: const NavigationMountId('app'),
        moduleId: const NavigationModuleId('app'),
        scopeId: const NavigationScopeId('root'),
      ),
    ],
  );
}

It finishes with one passing test, without opening an application window. SizedBox is deliberate: this checks the logical navigation pipeline, not visible Flutter UI.

Add it to an application

dependencies:
  ark_navigation: ^2.3.0
dev_dependencies:
  ark_navigation_test: ^1.0.0
  flutter_test:
    sdk: flutter

Use the companion only in development dependencies. The examples are checked against hosted Ark Navigation 2.3.0; no sibling path override is required.

Choose the tool for your question

Question Tool
Did an operation commit, reject or fail? The real ticket.committed or operation Future, optionally bounded by NavigationTestWait
What result did that entry return? The real ticket.completed, preserving its result type
Are all entries present in exactly this order? NavigationTestMatchers.hasStack on NavigationSnapshot
Is this the same retained page, rather than a replacement? Specify entryId in NavigationTestEntryExpectation
Which lifecycle callbacks and failures were observed? NavigationTestObserver
Does a real Flutter page appear and react to Back? NavigationSession, WidgetTester and Flutter finders; the observer can be attached without owning them

NavigationTestDriver creates and owns a real Controller, not a mock port, Session or root widget. Navigation calls stay on the normal API, for example destination.on(driver.controller).push().

Next: the complete Catalog scenario

Walk through guest access, sign-in, tabs and logout. The full executable test checks two Mounts of one module, a denied request, retained Entry identity, a typed pop and clearing only the private branch.

Widget testing uses a real WidgetsApp and nested Navigator. Its second test demonstrates that the caller must pump fake time for a timeout to elapse.

Ownership and observation

Register driver.close in addTearDown immediately. It closes only its own Controller, synchronously. An application resource may need a separate awaited cleanup acknowledgement; didExit alone does not prove that cleanup succeeded.

Observer event and failure views have different Dart types. Attach them at owner construction. A borrowed observer never disposes that owner. stop() freezes the recording window and leaves metadata readable.

Each channel retains 200 records by default. Dropped records and capture/wiring errors are visible; exact lifecycle/failure assertions refuse partial evidence. A Guard denial is not an unexpected failure. Failure metadata omits error values and stack traces; Entry metadata omits Destination values and concrete URIs. Application-defined IDs and patterns can still be sensitive.

Read the ownership and observation contract before treating any log as a complete history.

Timing and limits

Timeout stops waiting; it does not cancel Guard, navigation or cleanup. It does not turn a source TimeoutException into a helper timeout. There is no waitForIdle, automatic pump, hidden test-runner hook or global router.

Audit, Route Auditor, Inspector and Trace remain existing core tools. This package does not implement another validator or navigation algorithm. Its stack assertions do not certify screen visibility, physical gestures, real browser history or OS windows.

All local checks

flutter analyze
flutter test test example
dart doc

No Material/Cupertino runtime dependency, code generator, DI container or state manager is required.

Project and tooling

The package includes navigation-test instructions for coding assistants, usable with tools that discover Agent Skills. They preserve explicit ownership, time control and the boundaries of test evidence. They are instructions, not a runtime dependency or automatic test generator.

Libraries

ark_navigation_test
Real-controller fixtures and explicit observations for Ark Navigation tests.