#!/bin/bash

# This script contains a bunch of CBRAIN cli
# command that are used as part of a test framework.
# Each line here is evaluated separately and their
# result (stdout and stderr) carefully recorded. This
# is performed by the external wrapper script
# "capture_wrapper".
#
# The content of the test database, at the beginning of
# this test, is expected to match what was seeded
# by the CBRAIN test API code, as seen in
#
# https://github.com/aces/cbrain/blob/master/BrainPortal/db/seeds_test_api.rb
#
# Original author: Pierre Rioux <pierre.rioux@mcgill.ca>, July 2025

# ------------------
# Not logged in
# ------------------

./switch_session NONE

# Version commands
cbrain        version
cbrain --json version

# Connection commands
cbrain        whoami
cbrain --json whoami

# ------------------
# As normal user
# ------------------

./switch_session norm # logged in a normal user 'norm', id=2

# Version commands
cbrain        version
cbrain --json version

# Connection commands
cbrain        whoami
cbrain --json whoami

# Named session management
cbrain         session list

# Bourreaux
cbrain         remote-resource list
cbrain --json  remote-resource list
cbrain --jsonl remote-resource list

cbrain         remote-resource show 13
cbrain --json  remote-resource show 13
cbrain --jsonl remote-resource show 13

# Data Providers
cbrain         dataprovider list
cbrain --json  dataprovider list
cbrain --jsonl dataprovider list

cbrain         dataprovider show 15
cbrain --json  dataprovider show 15
cbrain --jsonl dataprovider show 15

# Projects (groups)
cbrain         project list
cbrain --json  project list
cbrain --jsonl project list

cbrain         project show 10
cbrain --json  project show 10
cbrain --jsonl project show 10

cbrain         project switch 10
cbrain         project unswitch

cbrain --json  project switch 10
cbrain --json  project unswitch

cbrain --jsonl project switch 10
cbrain --jsonl project unswitch

cbrain         project switch all
cbrain --json  project switch all
cbrain --jsonl project switch all
cbrain         project switch 3 # reset project filter for task list below

# Tags
cbrain         tag list
cbrain --json  tag list
cbrain --jsonl tag list

cbrain         tag show 21
cbrain --json  tag show 21
cbrain --jsonl tag show 21

cbrain         tag show   99
cbrain         tag update 99 --name Renamed --user-id 2 --group-id 3
cbrain         tag show   99
cbrain         tag delete 99 --yes

cbrain         tag create --name NewTag1 --user-id 2 --group-id 3
cbrain --json  tag create --name NewTag2 --user-id 2 --group-id 3
cbrain --jsonl tag create --name NewTag3 --user-id 2 --group-id 3

# Tasks
# Reset above to project 3 so task list matches seeded DB view.
cbrain         task list
cbrain --json  task list
cbrain --jsonl task list

cbrain         task show 2
cbrain --json  task show 2
cbrain --jsonl task show 2

# Task operations (hold on Completed task → skipped; deterministic, no BAC)
cbrain         task operation hold --task-id 2
cbrain --json  task operation hold --task-id 2
cbrain --jsonl task operation hold --task-id 2
cbrain         task operation hold  # missing --task-id / --batch-id

# ToolConfigs, as admin user
./switch_session admin
cbrain         session list
cbrain         tool-config list
cbrain --json  tool-config list
cbrain --jsonl tool-config list

cbrain         tool-config show 19 # not visible to normal user norm
cbrain --json  tool-config show 19
cbrain --jsonl tool-config show 19

# ToolConfigs, as normal user (CLI switch_session; both sessions already planted)
cbrain         switch_session norm
cbrain         session list
cbrain         tool-config list
cbrain --json  tool-config list
cbrain --jsonl tool-config list

cbrain         tool-config show 19 # visible to user admin
cbrain --json  tool-config show 19
cbrain --jsonl tool-config show 19

# Tools
cbrain         tool list
cbrain --json  tool list
cbrain --jsonl tool list

cbrain         tool show 17
cbrain --json  tool show 17
cbrain --jsonl tool show 17

# Background activities
cbrain         background list
cbrain --json  background list
cbrain --jsonl background list

cbrain         background show 43
cbrain --json  background show 43
cbrain --jsonl background show 43

# Userfiles
cbrain         file list
cbrain --json  file list
cbrain --jsonl file list

cbrain         file show 2
cbrain --json  file show 2
cbrain --jsonl file show 2

cbrain         file delete 5 --yes

cbrain         file move --file-id 2 --dp-id 15
#cbrain --json  file move --file-id 2 --dp-id 15

# Logging out
cbrain         logout
