// Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:isolate';

final packageUriToResolve = Uri.parse("package:foo/bar.dart");
final packageResolvedUri = "file:///no/such/directory/lib/bar.dart";

final packageConfigJson = """
{
  "configVersion": 2,
  "packages": [
    {
      "name": "foo",
      "rootUri": "file:///no/such/directory",
      "packageUri": "lib/",
      "languageVersion": "2.7"
    }
  ]
}
""";

main([args, port]) async {
  if (port != null) {
    testPackageResolution(port);
    return;
  }
  await runTest(packageConfigJson);
}

Future runTest(String packageConfig) async {
  final data = Uri.dataFromString(packageConfig);
  final port = ReceivePort();
  await Isolate.spawnUri(
    Platform.script,
    [],
    port.sendPort,
    packageConfig: data,
  );
  final msg = await port.first;
  if (msg is! List) {
    print(msg.runtimeType);
    throw "Failure return from spawned isolate:\n\n$msg";
  }
  if (msg[0] != data.toString()) {
    throw "Bad package config in child isolate: ${msg[0]}\n"
        "Expected: $data";
  }
  if (msg[1] != packageResolvedUri) {
    throw "Package path not matching: ${msg[1]}";
  }
  print("SUCCESS");
}

testPackageResolution(port) async {
  try {
    var packageConfigStr = Platform.packageConfig;
    var packageConfig = await Isolate.packageConfig;
    if (packageConfig != Isolate.packageConfigSync) {
      throw "Isolate.packageConfig != Isolate.packageConfigSync";
    }
    var resolvedPkg = await Isolate.resolvePackageUri(packageUriToResolve);
    if (resolvedPkg != Isolate.resolvePackageUriSync(packageUriToResolve)) {
      throw "Isolate.resolvePackageUri != Isolate.resolvePackageUriSync";
    }
    print("Spawned isolate's package config flag: $packageConfigStr");
    print("Spawned isolate's loaded package config: $packageConfig");
    print("Spawned isolate's resolved package path: $resolvedPkg");
    port.send([packageConfig?.toString(), resolvedPkg?.toString()]);
  } catch (e, s) {
    port.send("$e\n$s\n");
  }
}
