[flutter_tools] support run -d chrome test scripts (#51658)

diff --git a/dev/bots/test.dart b/dev/bots/test.dart
index 5c82140..b1b389b 100644
--- a/dev/bots/test.dart
+++ b/dev/bots/test.dart
@@ -590,7 +590,8 @@
 Future<void> _runWebIntegrationTests() async {
   await _runWebStackTraceTest('profile');
   await _runWebStackTraceTest('release');
-  await _runWebDebugStackTraceTest();
+  await _runWebDebugTest('lib/stack_trace.dart');
+  await _runWebDebugTest('test/test.dart');
 }
 
 Future<void> _runWebStackTraceTest(String buildMode) async {
@@ -636,7 +637,7 @@
 /// Debug mode is special because `flutter build web` doesn't build in debug mode.
 ///
 /// Instead, we use `flutter run --debug` and sniff out the standard output.
-Future<void> _runWebDebugStackTraceTest() async {
+Future<void> _runWebDebugTest(String target) async {
   final String testAppDirectory = path.join(flutterRoot, 'dev', 'integration_tests', 'web');
   final CapturedOutput output = CapturedOutput();
   bool success = false;
@@ -648,7 +649,8 @@
       '-d',
       'chrome',
       '--web-run-headless',
-      'lib/stack_trace.dart',
+      '-t',
+      target,
     ],
     output: output,
     outputMode: OutputMode.capture,
diff --git a/dev/integration_tests/web/lib/a.dart b/dev/integration_tests/web/lib/a.dart
new file mode 100644
index 0000000..4b209ef
--- /dev/null
+++ b/dev/integration_tests/web/lib/a.dart
@@ -0,0 +1,5 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+const String message = 'a';
diff --git a/dev/integration_tests/web/lib/b.dart b/dev/integration_tests/web/lib/b.dart
new file mode 100644
index 0000000..edb667e
--- /dev/null
+++ b/dev/integration_tests/web/lib/b.dart
@@ -0,0 +1,5 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+const String message = 'b';
diff --git a/dev/integration_tests/web/lib/c.dart b/dev/integration_tests/web/lib/c.dart
new file mode 100644
index 0000000..000e732
--- /dev/null
+++ b/dev/integration_tests/web/lib/c.dart
@@ -0,0 +1,5 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+const String message = 'c';
diff --git a/dev/integration_tests/web/lib/d.dart b/dev/integration_tests/web/lib/d.dart
new file mode 100644
index 0000000..c4a3de8
--- /dev/null
+++ b/dev/integration_tests/web/lib/d.dart
@@ -0,0 +1,5 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+const String message = 'd';
diff --git a/dev/integration_tests/web/test/test.dart b/dev/integration_tests/web/test/test.dart
new file mode 100644
index 0000000..e184f93
--- /dev/null
+++ b/dev/integration_tests/web/test/test.dart
@@ -0,0 +1,16 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:web_integration/a.dart'
+  if (dart.library.io) 'package:web_integration/b.dart' as message1;
+import 'package:web_integration/c.dart'
+  if (dart.library.html) 'package:web_integration/d.dart' as message2;
+
+void main() {
+  if (message1.message == 'a' && message2.message == 'd')  {
+    print('--- TEST SUCCEEDED ---');
+  } else {
+    print('--- TEST FAILED ---');
+  }
+}
diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
index 0750c53..6073301 100644
--- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
+++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
@@ -573,7 +573,13 @@
         .childFile('generated_plugin_registrant.dart')
         .absolute.path;
       final Uri generatedImport = packageUriMapper.map(generatedPath);
-      final String importedEntrypoint = packageUriMapper.map(main).toString() ?? 'file://$main';
+      String importedEntrypoint = packageUriMapper.map(main)?.toString();
+      // Special handling for entrypoints that are not under lib, such as test scripts.
+      if (importedEntrypoint == null) {
+        final String parent = globals.fs.file(main).parent.path;
+        flutterDevices.first.generator.addFileSystemRoot(parent);
+        importedEntrypoint = 'org-dartlang-app:///${globals.fs.path.basename(main)}';
+      }
 
       final String entrypoint = <String>[
         'import "$importedEntrypoint" as entrypoint;',
diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
index 7641d18..dde12a4 100644
--- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
@@ -427,9 +427,8 @@
     Usage: () => MockFlutterUsage(),
   }));
 
-  test('web resident runner iss debuggable', () => testbed.run(() {
+  test('web resident runner is debuggable', () => testbed.run(() {
     expect(residentWebRunner.debuggingEnabled, true);
-  }, overrides: <Type, Generator>{
   }));
 
   test('Exits when initial compile fails', () => testbed.run(() async {