def main()

in inference/scripts/test-wasm.py [0:0]


def main():
    parser = argparse.ArgumentParser(
        description="Test WASM by building and handling artifacts.",
        formatter_class=argparse.RawTextHelpFormatter,
    )

    parser.add_argument("--clobber", action="store_true", help="Clobber the build artifacts")
    parser.add_argument(
        "--force-rebuild", action="store_true", help="Force rebuilding the artifacts"
    )
    parser.add_argument(
        "--debug",
        action="store_true",
        help="Build with debug symbols, useful for profiling",
    )
    parser.add_argument(
        "-j",
        type=int,
        help="Number of cores to use for building (default: all available cores)",
    )
    args = parser.parse_args()

    build_wasm_script = os.path.join(SCRIPTS_PATH, "build-wasm.py")
    build_command = [sys.executable, build_wasm_script]
    if args.clobber:
        build_command.append("--clobber")
    if args.force_rebuild:
        build_command.append("--force-rebuild")
    if args.debug:
        build_command.append("--debug")
    if args.j:
        build_command.extend(["-j", str(args.j)])

    print("\nšŸš€ Starting build-wasm.py")
    subprocess.run(build_command, check=True)

    print("\nšŸ“„ Pulling translations model files with git lfs\n")
    subprocess.run(["git", "lfs", "pull"], cwd=MODELS_PATH, check=True)
    print(f"   Pulled all files in {MODELS_PATH}")

    print("\nšŸ“ Copying generated build artifacts to the WASM test directory\n")

    os.makedirs(GENERATED_PATH, exist_ok=True)
    shutil.copy2(WASM_ARTIFACT, GENERATED_PATH)
    shutil.copy2(JS_ARTIFACT, GENERATED_PATH)

    print(f"   Copied the following artifacts to {GENERATED_PATH}:")
    print(f"     - {JS_ARTIFACT}")
    print(f"     - {WASM_ARTIFACT}")

    print(f"\nšŸ”‘ Calculating SHA-256 hash of {JS_ARTIFACT}\n")
    hash_value = calculate_sha256(JS_ARTIFACT)
    with open(JS_ARTIFACT_HASH, "w") as hash_file:
        hash_file.write(f"{hash_value}  {os.path.basename(JS_ARTIFACT)}\n")
    print(f"   Hash of {JS_ARTIFACT} written to")
    print(f"   {JS_ARTIFACT_HASH}")

    print("\nšŸ“‚ Decompressing model files required for WASM testing\n")
    subprocess.run(["gzip", "-dkrf", MODELS_PATH], check=True)
    print(f"   Decompressed models in {MODELS_PATH}\n")

    print("\nšŸ”§ Installing npm dependencies for WASM JS tests\n")
    subprocess.run(["npm", "install"], cwd=WASM_TESTS_PATH, check=True)

    print("\nšŸ“Š Running Translations WASM JS tests\n")
    subprocess.run(["npm", "run", "test"], cwd=WASM_TESTS_PATH, check=True)

    print("\nāœ… test-wasm.py completed successfully.\n")