(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2021 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open Util

let%expect_test _ =
  let p =
    {|
let () =
  let cwd = Sys.getcwd() in
  if cwd = "/static/"
  then print_endline "without node filesystem"
  else print_endline "with node filesystem";
  try
    let _files = Sys.readdir "." in
    print_endline "Sys.readdir ok";
    let rec get_root s =
      let parent = Filename.dirname s in
      if s = parent then s else get_root parent
    in
    let files = Array.to_list (Sys.readdir (get_root cwd)) in
    (match files with
    | [] -> print_endline "empty root"
    | _ -> ())
  with
  | e ->
    print_endline @@ Printexc.to_string e;
    ()
  |}
  in
  compile_and_run p;
  [%expect {|
    with node filesystem
    Sys.readdir ok|}];
  compile_and_run p ~flags:[ "--target-env=isomorphic" ];
  [%expect {|
    with node filesystem
    Sys.readdir ok|}];
  compile_and_run p ~flags:[ "--target-env=nodejs" ];
  [%expect {|
    with node filesystem
    Sys.readdir ok|}];
  compile_and_run p ~flags:[ "--target-env=browser" ];
  [%expect {|
    without node filesystem
    Sys.readdir ok
    empty root |}]

module Jsoo = Js_of_ocaml_compiler
module StringSet = Set.Make (String)

class find_require r =
  object
    inherit Jsoo.Js_traverse.iter as super

    method! expression e =
      (match e with
      | ECall (EVar (Jsoo.Javascript.S { name = Utf8 "require"; loc; _ }), _, args, _)
        -> (
          match args with
          | [ Arg name ] -> (
              match name with
              | EStr (Utf8 name) -> r := (name, loc) :: !r
              | _ -> r := ("<Unknown>", loc) :: !r)
          | _ -> assert false)
      | _ -> ());
      super#expression e
  end

(* Check target-env=browser doesn't use 'require("node:...")' *)
let%expect_test _ =
  let test flags =
    let r = ref [] in
    let o = new find_require r in
    let p = compile_and_parse_whole_program ~flags "" in
    o#program p;
    let ss =
      List.fold_left
        (fun acc (x, _loc) ->
          let x = if String.starts_with ~prefix:"node:" x then "node" else x in
          StringSet.add x acc)
        StringSet.empty
        !r
    in
    StringSet.iter (fun name -> Printf.eprintf "%s\n" name) ss
  in
  test [ "--linkall" ];
  [%expect {| node |}];
  test [ "--linkall"; "--target-env=browser" ];
  [%expect {| |}]
