load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//javascript/node/selenium-webdriver:eslint/package_json.bzl", eslint_bin = "bin")
load("@npm//javascript/node/selenium-webdriver:prettier/package_json.bzl", prettier_bin = "bin")
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
load("//common:defs.bzl", "copy_file")
load("//javascript:defs.bzl", "mocha_test")
load("//javascript/private:browsers.bzl", "BROWSERS")

npm_link_all_packages(name = "node_modules")

VERSION = "4.27.0-nightly202410301443"

BROWSER_VERSIONS = [
    "v85",
    "v128",
    "v129",
    "v130",
]

js_library(
    name = "prod-src-files",
    srcs = [
        "CHANGES.md",
        "README.md",
        "package.json",
    ] + glob([
        "*.js",
        "example/*.js",
        "http/*.js",
        "io/*.js",
        "lib/*.js",
        "lib/atoms/bidi-mutation-listener.js",
        "net/*.js",
        "remote/*.js",
        "testing/*.js",
        "devtools/*.js",
        "common/*.js",
        "bidi/*.js",
    ]),
    deps = [
        ":node_modules/@bazel/runfiles",
        ":node_modules/jszip",
        ":node_modules/tmp",
        ":node_modules/ws",
    ],
)

npm_package(
    name = "selenium-webdriver",
    srcs = [
        ":license",
        ":manager-linux",
        ":manager-macos",
        ":manager-windows",
        ":prod-src-files",
        "//javascript/node/selenium-webdriver/lib/atoms:bidi-mutation-listener",
        "//javascript/node/selenium-webdriver/lib/atoms:find-elements",
        "//javascript/node/selenium-webdriver/lib/atoms:get_attribute",
        "//javascript/node/selenium-webdriver/lib/atoms:is_displayed",
        "//javascript/node/selenium-webdriver/lib/atoms:mutation-listener",
    ],
    package = "selenium-webdriver",
    publishable = True,
    tags = [
        "release-artifact",
    ],
    version = VERSION,
    visibility = ["//visibility:public"],
)

pkg_tar(
    name = "selenium-webdriver.tgz",
    srcs = [
        ":selenium-webdriver",
    ],
    out = "selenium-webdriver-%s.tgz" % VERSION,
    extension = "tgz",
    package_dir = "package",
    strip_prefix = "selenium-webdriver",
)

SMALL_TESTS = [
    "test/io/io_test.js",
    "test/io/zip_test.js",
    "test/lib/by_test.js",
    "test/lib/credentials_test.js",
    "test/lib/error_test.js",
    "test/lib/http_test.js",
    "test/lib/input_test.js",
    "test/lib/logging_test.js",
    "test/lib/promise_test.js",
    "test/lib/until_test.js",
    "test/lib/virtualauthenticatoroptions_test.js",
    "test/lib/webdriver_test.js",
    "test/net/index_test.js",
    "test/net/portprober_test.js",
]

LARGE_TESTS = glob(
    ["test/**/*_test.js"],
    exclude = SMALL_TESTS,
)

js_library(
    name = "small-test-srcs",
    srcs = SMALL_TESTS,
)

js_library(
    name = "large-test-srcs",
    srcs = LARGE_TESTS,
)

js_library(
    name = "test-data",
    srcs = glob(
        [
            "lib/test/**",
            "test/**",
        ],
        exclude = [
            "test/**/*_test.js",
        ],
    ),
    deps = [
        ":node_modules/@bazel/runfiles",
    ],
)

mocha_test(
    name = "small-tests",
    size = "small",
    args = ["./**/*test.js"],
    chdir = package_name(),
    data = [
        ":node_modules/@bazel/runfiles",
        ":node_modules/mocha",
        ":node_modules/mocha-junit-reporter",
        ":node_modules/sinon",
        ":small-test-srcs",
        ":test-data",
        "//:node_modules/selenium-webdriver",
        "//common/extensions:js-lib",
        "//common/src/web:js-lib",
    ],
)

[
    [
        mocha_test(
            name = "%s-%s" % (
                test.replace("/", "-").replace("_", "-"),
                browser,
            ),
            size = "large",
            args = [
                "--retries",
                "1",
                test,
            ],
            chdir = package_name(),
            data = [
                ":large-test-srcs",
                ":node_modules/express",
                ":node_modules/mocha",
                ":node_modules/mocha-junit-reporter",
                ":node_modules/multer",
                ":node_modules/serve-index",
                ":node_modules/sinon",
                ":test-data",
                "//:node_modules/selenium-webdriver",
                "//common/extensions:js-lib",
                "//common/src/web:js-lib",
            ] + BROWSERS[browser]["data"],
            env = {
                "SELENIUM_BROWSER": browser,
            } | BROWSERS[browser]["env"],
            tags = [
                browser,
                "browser-test",
                "no-sandbox",
                "requires-network",
            ],
        ),
    ]
    for test in LARGE_TESTS
    for browser in BROWSERS.keys()
]

# npm_package does not pick up filegroup sources.
genrule(
    name = "license",
    srcs = ["//:license"],
    outs = [
        "LICENSE",
        "NOTICE",
    ],
    cmd = "cp $(locations //:license) $(@D)",
)

copy_file(
    name = "manager-linux",
    src = "//common/manager:selenium-manager-linux",
    out = "bin/linux/selenium-manager",
)

copy_file(
    name = "manager-macos",
    src = "//common/manager:selenium-manager-macos",
    out = "bin/macos/selenium-manager",
)

copy_file(
    name = "manager-windows",
    src = "//common/manager:selenium-manager-windows",
    out = "bin/windows/selenium-manager.exe",
)

copy_to_bin(
    name = "package-json",
    srcs = ["package.json"],
)

eslint_bin.eslint_test(
    name = "eslint-test",
    chdir = package_name(),
    data = [
        ":eslint-config",
        ":node_modules/@eslint/js",
        ":node_modules/eslint-plugin-mocha",
        ":node_modules/eslint-plugin-n",
        ":node_modules/eslint-plugin-no-only-tests",
        ":node_modules/eslint-plugin-prettier",
        ":node_modules/globals",
        ":node_modules/jszip",
        ":node_modules/mocha",
        ":node_modules/tmp",
        ":node_modules/ws",
        ":package-json",
        ":prod-src-files",
    ],
    tags = [
        "lint",
    ],
)

copy_to_bin(
    name = "eslint-config",
    srcs = ["eslint.config.js"],
)

prettier_bin.prettier_test(
    name = "prettier-test",
    args = [
        ".",
        "--check",
        "--ignore-unknown",
        "--config=.prettierrc",
    ],
    chdir = package_name(),
    data = [
        ":large-test-srcs",
        ":prettier-config",
        ":prettier-ignore",
        ":prod-src-files",
        ":small-test-srcs",
    ],
    tags = [
        "lint",
    ],
)

copy_to_bin(
    name = "prettier-config",
    srcs = [".prettierrc"],
)

copy_to_bin(
    name = "prettier-ignore",
    srcs = [".prettierignore"],
)
