#!/usr/bin/env bats

load ../utils/_

setup() {
  standard_setup
}

teardown() {
  dfx_stop

  standard_teardown
}

@test "dfx generate creates files" {
  dfx_new hello
  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_exists "src/declarations/hello_backend/index.js"
  assert_file_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate creates only JS files" {
  dfx_new hello
  jq '.canisters.hello_backend.declarations.bindings=["js"]' dfx.json | sponge dfx.json

  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_exists "src/declarations/hello_backend/index.js"
  assert_file_not_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate creates only TS files" {
  dfx_new hello
  jq '.canisters.hello_backend.declarations.bindings=["ts"]' dfx.json | sponge dfx.json

  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_not_exists "src/declarations/hello_backend/index.js"
  assert_file_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate creates only JS & TS files" {
  dfx_new hello
  jq '.canisters.hello_backend.declarations.bindings=["js", "ts"]' dfx.json | sponge dfx.json

  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_exists "src/declarations/hello_backend/index.js"
  assert_file_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate creates only DID files" {
  dfx_new hello
  jq '.canisters.hello_backend.declarations.bindings=["did"]' dfx.json | sponge dfx.json

  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_not_exists "src/declarations/hello_backend/index.js"
  assert_file_not_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate does not create any files" {
  dfx_new hello
  jq '.canisters.hello_backend.declarations.bindings=[]' dfx.json | sponge dfx.json

  dfx_start
  dfx canister create --all
  dfx build
  dfx canister install --all

  dfx generate

  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.js"
  assert_file_not_exists "src/declarations/hello_backend/hello_backend.did.d.ts"
  assert_file_not_exists "src/declarations/hello_backend/index.js"
  assert_file_not_exists "src/declarations/hello_backend/index.d.ts"
}

@test "dfx generate succeeds with an encrypted identity without input" {
  dfx_new hello
  dfx_start
  dfx canister create --all

  assert_command "${BATS_TEST_DIRNAME}/../assets/expect_scripts/init_alice_with_pw.exp"
  assert_command dfx identity use alice

  assert_command timeout 30s dfx generate
}

@test "dfx generate does not require canister IDs for non-Motoko canisters" {
  dfx_new_rust hello
  assert_command dfx generate
}

@test "dfx generate does not require canister IDs for Motoko canisters" {
  dfx_new hello
  assert_command dfx generate
}

@test "dfx generate --network is accepted" {
  dfx_new hello
  assert_command dfx generate --network local
  assert_file_exists ".dfx/local/canisters/hello_backend/service.did"

  assert_command dfx generate --playground
  assert_file_exists ".dfx/playground/canisters/hello_backend/service.did"
}

@test "dfx generate does not delete source candid file of Rust canister when bindings contains no did" {
  dfx_new_rust hello
  jq '.canisters.hello_backend.declarations.bindings=["js"]' dfx.json | sponge dfx.json
  assert_command dfx generate
  assert_file_exists "src/hello_backend/hello_backend.did"
}
