<commit_before>open OUnit

module TestInterface = struct
  let service_name = "test_interface"

  exception Does_not_exist of (string * string)
  exception Cancelled of string

  module Task = struct
    type id = string
    type async_result = string [@@deriving rpc]
    type completion_t = {
      duration : float;
      result: async_result option;
    }
    type state =
      | Pending of float
      | Completed of completion_t
      | Failed of Rpc.t
    type t = {
      id: id;
      dbg: string;
      ctime: float;
      state: state;
      subtasks: (string * state) list;
      debug_info: (string * string) list;
      backtrace: string;
    }
  end

  module Exception = struct
    type exnty =
      | Internal_error of string
      | Does_not_exist of string * string
      | Cancelled of string
      | Unknown of string [@@deriving rpc]
  end

  exception Internal_error of string

  let exnty_of_exn = function
    | Internal_error s -> Exception.Internal_error s
    | Cancelled s -> Exception.Cancelled s
    | Does_not_exist (x,y) -> Exception.Does_not_exist (x,y)
    | e -> Exception.Unknown (Printexc.to_string e)
  let exn_of_exnty = function
    | Exception.Internal_error s -> Internal_error s
    | Exception.Does_not_exist (x,y) -> Does_not_exist (x,y)
    | Exception.Cancelled s -> Cancelled s
    | Exception.Unknown s -> Failure s
end

module T = Task_server.Task(TestInterface)

(* Test that we can add a task and retrieve it from the task list *)
let test_add () =
  let t = T.empty () in
  let task = T.add t "dbg" (fun _task -> Some "done") in
  let ts = T.list t in
  assert_bool "Task in task list" (List.mem task ts)

(* Test that destroying a task removes it from the task list *)
let test_destroy () =
  let t = T.empty () in
  let task = T.add t "dbg" (fun _task -> Some "done") in
  T.destroy task;
  let ts = T.list t in
  assert_bool "Task not in task list" (not (List.mem task ts))

(* Test 'running' a task, and that the various times associated with the
   task make sense, and that the task status is correctly completed with
   correct result *)
let test_run () =
  let t = T.empty () in
  let start = Unix.gettimeofday () in
  Thread.delay 0.001;
  let task = T.add t "dbg" (fun _task -> Thread.delay 0.001; Some "done") in<commit_msg>Fix unit tests: add missing functions<commit_after>open OUnit

module TestInterface = struct
  let service_name = "test_interface"

  exception Does_not_exist of (string * string)
  exception Cancelled of string
  let does_not_exist (a,b) = raise @@ Does_not_exist(a,b)
  let cancelled a = raise @@ Cancelled a

  module Task = struct
    type id = string
    type async_result = string [@@deriving rpc]
    type completion_t = {
      duration : float;
      result: async_result option;
    }
    type state =
      | Pending of float
      | Completed of completion_t
      | Failed of Rpc.t
    type t = {
      id: id;
      dbg: string;
      ctime: float;
      state: state;
      subtasks: (string * state) list;
      debug_info: (string * string) list;
      backtrace: string;
    }
  end

  module Exception = struct
    type exnty =
      | Internal_error of string
      | Does_not_exist of string * string
      | Cancelled of string
      | Unknown of string [@@deriving rpc]
  end

  exception Internal_error of string

  let exnty_of_exn = function
    | Internal_error s -> Exception.Internal_error s
    | Cancelled s -> Exception.Cancelled s
    | Does_not_exist (x,y) -> Exception.Does_not_exist (x,y)
    | e -> Exception.Unknown (Printexc.to_string e)
  let exn_of_exnty = function
    | Exception.Internal_error s -> Internal_error s
    | Exception.Does_not_exist (x,y) -> Does_not_exist (x,y)
    | Exception.Cancelled s -> Cancelled s
    | Exception.Unknown s -> Failure s

  let marshal_exn e = e |> exnty_of_exn |> Exception.rpc_of_exnty
end

module T = Task_server.Task(TestInterface)

(* Test that we can add a task and retrieve it from the task list *)
let test_add () =
  let t = T.empty () in
  let task = T.add t "dbg" (fun _task -> Some "done") in
  let ts = T.list t in
  assert_bool "Task in task list" (List.mem task ts)

(* Test that destroying a task removes it from the task list *)
let test_destroy () =
  let t = T.empty () in
  let task = T.add t "dbg" (fun _task -> Some "done") in
  T.destroy task;
  let ts = T.list t in
  assert_bool "Task not in task list" (not (List.mem task ts))

(* Test 'running' a task, and that the various times associated with the
   task make sense, and that the task status is correctly completed with
   correct result *)
let test_run () =
  let t = T.empty () in
  let start = Unix.gettimeofday () in
  Thread.delay 0.001;
  let task = T.add t "dbg" (fun _task -> Thread.delay 0.001; Some "done") in
{'fraction_non_alphanumeric': 0.0924140821458508, 'fraction_numerical': 0.003352891869237217, 'mean_word_length': 2.8368167202572345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2334074', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1538, 'n_words': 563}