resolveFile static method

Future<Map<String, Map<String, dynamic>>> resolveFile({
  1. required String testAssetPath,
  2. required String mockFilePath,
  3. required Future<String> assetLoader(
    1. String assetPath
    ),
  4. required String resolveAssetPath(
    1. String fromAssetPath,
    2. String relativePath
    ),
  5. Set<String>? resolving,
})

Loads and fully resolves a mock file into API response maps (statusCode / body / headers / delayMs / responses).

testAssetPath is the owning *.test.yaml (or equivalent) used to resolve $extends paths the same way as test-level mocks: file entries.

Implementation

static Future<Map<String, Map<String, dynamic>>> resolveFile({
  required String testAssetPath,
  required String mockFilePath,
  required Future<String> Function(String assetPath) assetLoader,
  required String Function(String fromAssetPath, String relativePath)
      resolveAssetPath,
  Set<String>? resolving,
}) async {
  final assetPath = resolveAssetPath(testAssetPath, mockFilePath);
  final stack = resolving ?? <String>{};
  if (!stack.add(assetPath)) {
    throw EnsembleTestFailure(
      'Mock file "$assetPath" has a cyclic ${extendsKey} chain: '
      '${[...stack, assetPath].join(' -> ')}',
    );
  }

  try {
    if (!assetPath.endsWith('.mock.json')) {
      throw EnsembleTestFailure(
        'Mock file "$mockFilePath" must be a .mock.json file.',
      );
    }

    final content = await assetLoader(assetPath);
    final dynamic doc = _parseJson(content, assetPath);
    if (doc == null) return {};
    if (doc is! Map) {
      throw EnsembleTestFailure('Mock file "$assetPath" root must be a map');
    }

    return resolveDocument(
      Map<dynamic, dynamic>.from(doc),
      sourceLabel: assetPath,
      testAssetPath: testAssetPath,
      assetLoader: assetLoader,
      resolveAssetPath: resolveAssetPath,
      resolving: stack,
    );
  } finally {
    stack.remove(assetPath);
  }
}