diff --git a/README.md b/README.md index 47738a1..5ef0eec 100644 --- a/README.md +++ b/README.md @@ -373,9 +373,10 @@ dailybot checkin questions add --type text --question "Focus tod dailybot checkin questions reorder ``` -> **Question types:** `text`, `multiple_choice`, `boolean`, `numeric` (the complete -> catalog). `multiple_choice` needs `--options`; `boolean` auto-generates Yes/No -> (don't pass options). Up to 50 questions per form/check-in. +> **Question types:** `text`, `multiple_choice`, `boolean`, `numeric`; forms also +> take `file` (an image or PDF people attach; never the blocker). `multiple_choice` +> needs `--options`; `boolean` auto-generates Yes/No (don't pass options). Up to 50 +> questions per form/check-in. > > **Per-question extras** (on `questions add`/`edit`, forms and check-ins): > `--short-question` (title shown in web & chat reports), `--variation` (repeatable diff --git a/dailybot_cli/commands/authoring_helpers.py b/dailybot_cli/commands/authoring_helpers.py index 822860f..a64f68d 100644 --- a/dailybot_cli/commands/authoring_helpers.py +++ b/dailybot_cli/commands/authoring_helpers.py @@ -22,6 +22,9 @@ # Question types accepted by the server. Anything else is rejected with 400. VALID_QUESTION_TYPES: tuple[str, ...] = ("text", "multiple_choice", "boolean", "numeric") +# Tables (forms) also take a ``file`` question: people attach images or PDFs. +# Check-ins can't ask for an upload, so the server refuses it there. +FORM_QUESTION_TYPES: tuple[str, ...] = (*VALID_QUESTION_TYPES, "file") # Server-side ceiling; mirrored here so the CLI fails fast on obviously-too-many. MAX_QUESTIONS: int = 50 # Report-channel cap (forms + check-ins); server rejects > 3 with too_many_report_channels. @@ -202,6 +205,7 @@ def build_question( short_question: str | None = None, variations: list[str] | None = None, logic: dict[str, Any] | None = None, + allowed_types: tuple[str, ...] = VALID_QUESTION_TYPES, ) -> dict[str, Any]: """Build a validated question payload (explicit ``question_type``/``question``). @@ -211,13 +215,16 @@ def build_question( ``short_question`` (report title), ``variations`` (alternate phrasings), and ``logic`` (conditional branching) are optional per-question extras; callers pass already-validated values (see ``resolve_question_extras``). + ``allowed_types`` is ``VALID_QUESTION_TYPES`` for check-ins and + ``FORM_QUESTION_TYPES`` (adds ``file``) for tables. """ qtype: str = question_type.strip().lower() - if qtype not in VALID_QUESTION_TYPES: + if qtype not in allowed_types: raise AuthoringError( - f"Invalid question type '{question_type}'. " - f"Choose from: {', '.join(VALID_QUESTION_TYPES)}." + f"Invalid question type '{question_type}'. Choose from: {', '.join(allowed_types)}." ) + if qtype == "file" and is_blocker: + raise AuthoringError("A file question can't be the blocker question.") text: str = question.strip() if not text: raise AuthoringError("Question text is required.") @@ -528,11 +535,14 @@ def resolve_question_extras( return extras -def parse_questions_file(path: str) -> list[dict[str, Any]]: +def parse_questions_file( + path: str, allowed_types: tuple[str, ...] = VALID_QUESTION_TYPES +) -> list[dict[str, Any]]: """Load and validate a JSON array of question objects from ``path``. Accepts both field conventions (``question_type``/``question`` or - ``type``/``label``) and enforces ``MAX_QUESTIONS``. + ``type``/``label``) and enforces ``MAX_QUESTIONS``. Tables pass + ``FORM_QUESTION_TYPES`` so ``file`` questions are accepted. """ try: raw: str = Path(path).read_text(encoding="utf-8") @@ -583,6 +593,7 @@ def parse_questions_file(path: str) -> list[dict[str, Any]]: options=options, required=required, is_blocker=is_blocker, + allowed_types=allowed_types, **extras, ) ) @@ -1019,7 +1030,10 @@ def resolve_form_config( ) -def build_questions_interactively(ai_short_question: bool = False) -> list[dict[str, Any]]: +def build_questions_interactively( + ai_short_question: bool = False, + allowed_types: tuple[str, ...] = VALID_QUESTION_TYPES, +) -> list[dict[str, Any]]: """Walk the user through building questions with questionary prompts. Every question passes through ``build_question``, so the interactive path @@ -1036,9 +1050,7 @@ def build_questions_interactively(ai_short_question: bool = False) -> list[dict[ questions: list[dict[str, Any]] = [] while len(questions) < MAX_QUESTIONS: - qtype: str | None = questionary.select( - "Question type:", choices=list(VALID_QUESTION_TYPES) - ).ask() + qtype: str | None = questionary.select("Question type:", choices=list(allowed_types)).ask() if qtype is None: break text: str | None = questionary.text("Question text:").ask() @@ -1073,6 +1085,7 @@ def build_questions_interactively(ai_short_question: bool = False) -> list[dict[ required=bool(required), is_blocker=bool(is_blocker), short_question=short_question, + allowed_types=allowed_types, ) ) except AuthoringError as exc: diff --git a/dailybot_cli/commands/form.py b/dailybot_cli/commands/form.py index a221322..329f245 100644 --- a/dailybot_cli/commands/form.py +++ b/dailybot_cli/commands/form.py @@ -12,6 +12,7 @@ DailyBotClient, ) from dailybot_cli.commands.authoring_helpers import ( + FORM_QUESTION_TYPES, build_question, build_question_edit_fields, build_questions_interactively, @@ -1045,9 +1046,11 @@ def form_create( client = require_auth() check_report_channels(report_channels) if interactive: - questions: list[dict[str, Any]] | None = build_questions_interactively(ai_short_question) + questions: list[dict[str, Any]] | None = build_questions_interactively( + ai_short_question, allowed_types=FORM_QUESTION_TYPES + ) elif questions_file: - questions = parse_questions_file(questions_file) + questions = parse_questions_file(questions_file, allowed_types=FORM_QUESTION_TYPES) else: questions = None require_questions(questions, "form") @@ -1238,7 +1241,10 @@ def form_questions_list(form_uuid: str, json_mode: bool) -> None: @form_questions.command("add") @click.argument("form_uuid") @click.option( - "--type", "question_type", required=True, help="text/multiple_choice/boolean/numeric." + "--type", + "question_type", + required=True, + help="text/multiple_choice/boolean/numeric/file (file: an image or PDF people attach).", ) @click.option("--question", required=True, help="The question text.") @click.option("--options", default=None, help="Comma-separated options (multiple_choice only).") @@ -1283,6 +1289,8 @@ def form_questions_add( --question "Rating?" --options "Excellent,Good,Average,Poor" --ai-short-question dailybot form questions add --type boolean --question "Ship it?" \\ --short-question "Ship" --jump-if-equals "No" --jump-to 4 + dailybot form questions add --type file --question "Screenshot" \\ + --short-question "Screenshot" --optional """ client = require_auth() require_short_question(extra_flags.get("short_question"), ai_short_question) @@ -1293,6 +1301,7 @@ def form_questions_add( options=parse_options(options), required=required, is_blocker=is_blocker, + allowed_types=FORM_QUESTION_TYPES, **extras, ) if ai_short_question: @@ -1314,7 +1323,12 @@ def form_questions_add( @click.argument("form_uuid") @click.argument("question_uuid") @click.option("--question", default=None, help="New question text.") -@click.option("--type", "question_type", default=None, help="New question type.") +@click.option( + "--type", + "question_type", + default=None, + help="New question type (text/multiple_choice/boolean/numeric/file).", +) @click.option("--options", default=None, help="New comma-separated options (multiple_choice).") @click.option("--required/--optional", "required", default=None, help="Toggle required.") @click.option( diff --git a/dailybot_cli/commands/public_api_helpers.py b/dailybot_cli/commands/public_api_helpers.py index 80553d4..4029ab0 100644 --- a/dailybot_cli/commands/public_api_helpers.py +++ b/dailybot_cli/commands/public_api_helpers.py @@ -361,7 +361,8 @@ "response_date_format_is_invalid": "Invalid date. Use YYYY-MM-DD.", # Forms & check-ins authoring (create/edit/questions/responses) "invalid_question_type": ( - "Invalid question type. Use one of: text, multiple_choice, boolean, numeric." + "Invalid question type. Use one of: text, multiple_choice, boolean, numeric " + "(forms also take file)." ), "multiple_choice_requires_options": ( "Multiple-choice questions need at least one option (--options)." diff --git a/docs/API_REFERENCE.md b/docs/API_REFERENCE.md index c3102b2..fa300a2 100644 --- a/docs/API_REFERENCE.md +++ b/docs/API_REFERENCE.md @@ -257,7 +257,7 @@ Creating and configuring forms/check-ins (as opposed to filling them in). All au | Command | HTTP | Notes | |---|---|---| | `form list [--include-archived]` | `GET /v1/forms/` (`?include_archived=true`) | Archived forms hidden by default; flagged in a Status column when shown. | -| `form create -n NAME [--questions-file F] [--interactive] [--report-channel UUID] [config flags]` | `POST /v1/forms/create/` (`name`, `questions?`, `report_channels?`, config all inline) | Question types: `text`, `multiple_choice`, `boolean`, `numeric`; ≤ 50. Config flags below. | +| `form create -n NAME [--questions-file F] [--interactive] [--report-channel UUID] [config flags]` | `POST /v1/forms/create/` (`name`, `questions?`, `report_channels?`, config all inline) | Question types: `text`, `multiple_choice`, `boolean`, `numeric`, `file` (forms only); ≤ 50. Config flags below. | | `form edit [--name] [--report-channel]` | `PATCH /v1/forms//config/` | Thin subset of `form config`. | | `form config [--name] [--report-channel] [config flags]` | `PATCH /v1/forms//config/` | Full form config (partial). Flags below. | @@ -269,7 +269,7 @@ Creating and configuring forms/check-ins (as opposed to filling them in). All au **Report title is required by the CLI.** When authoring a question (`questions add`, or seeding via `create --questions-file`/`--interactive`), a `--short-question` / `"short_question"` is **mandatory** — pass `--ai-short-question` to opt into Dailybot's AI generating it instead (AI titling only runs on intelligence-enabled check-ins). Server-side AI titling is a frontend-oriented convenience; agents/scripts should author explicit report titles. (`questions edit` doesn't require it — partial update.) -**Per-question extras** (on `questions add` + `edit`, both forms and check-ins): `--short-question` (title shown in web & chat reports, ≤512 chars), `--variation` (alternate phrasing rotated per run; repeatable, ≤10), and question logic via `--logic-file ` (a JSON `{"rules": {"rules_if": [...], "rules_else": {...}}}` object) or the inline single-jump shortcut `--jump-if-equals VALUE --jump-to N [--else-jump-to M]` (`N`/`M` = target question index, `-1` = end). A **`rules_else` fallback is required** and jump targets are **forward-only** (must be greater than the question's own index, or `-1`); the server owns the question index and rejects backward/out-of-range jumps. Navigation keys (`question_key`/`next_key`/`prev_key`) are computed server-side — never send them. Logic operators (server enforces per type): text — `is_equal_to`, `is_not_equal_to`, `contains`, `not_contains`, `begins_with`, `not_begins_with`, `ends_with`, `not_ends_with`; numeric — `is_equal_to`, `is_not_equal_to`, `lower_than`, `lower_or_equal_than`, `greater_than`, `greater_or_equal_than`; `multiple_choice`/`boolean` — `is_equal_to`, `is_not_equal_to`. Boolean comparison values are JSON `true`/`false` (the CLI coerces `--jump-if-equals true/false`). Actions: `jump_to` (int target), `trigger_checkin` / `trigger_form` (UUID target); connectors: `and`, `or` (`multiple_choice`: `or` only). Empty question text is rejected server-side (`question_label_required`); both check-ins and forms **require at least one question at create time** (`questions_required`) — the CLI fails fast if you create without `--questions-file`/`--interactive`. Question types remain `text`, `multiple_choice`, `boolean`, `numeric` (the complete catalog). +**Per-question extras** (on `questions add` + `edit`, both forms and check-ins): `--short-question` (title shown in web & chat reports, ≤512 chars), `--variation` (alternate phrasing rotated per run; repeatable, ≤10), and question logic via `--logic-file ` (a JSON `{"rules": {"rules_if": [...], "rules_else": {...}}}` object) or the inline single-jump shortcut `--jump-if-equals VALUE --jump-to N [--else-jump-to M]` (`N`/`M` = target question index, `-1` = end). A **`rules_else` fallback is required** and jump targets are **forward-only** (must be greater than the question's own index, or `-1`); the server owns the question index and rejects backward/out-of-range jumps. Navigation keys (`question_key`/`next_key`/`prev_key`) are computed server-side — never send them. Logic operators (server enforces per type): text — `is_equal_to`, `is_not_equal_to`, `contains`, `not_contains`, `begins_with`, `not_begins_with`, `ends_with`, `not_ends_with`; numeric — `is_equal_to`, `is_not_equal_to`, `lower_than`, `lower_or_equal_than`, `greater_than`, `greater_or_equal_than`; `multiple_choice`/`boolean` — `is_equal_to`, `is_not_equal_to`. Boolean comparison values are JSON `true`/`false` (the CLI coerces `--jump-if-equals true/false`). Actions: `jump_to` (int target), `trigger_checkin` / `trigger_form` (UUID target); connectors: `and`, `or` (`multiple_choice`: `or` only). Empty question text is rejected server-side (`question_label_required`); both check-ins and forms **require at least one question at create time** (`questions_required`) — the CLI fails fast if you create without `--questions-file`/`--interactive`. Question types: `text`, `multiple_choice`, `boolean`, `numeric`; forms also take `file` (an attachment; optional or required, never the blocker), which the server refuses on check-ins. | `form questions delete ` | `DELETE /v1/forms//questions//delete/` | Confirms unless `--yes`. | | `form questions reorder ...` | `PUT /v1/forms//questions/reorder/` | Unknown UUID → 400. | | `checkin create -n NAME [--time --days --timezone \| --schedule-file] [--user --team] [--questions-file \| --interactive] [--report-channel]` | `POST /v1/checkins/create/` | `days` = ISO weekday ints (0=Sun…6=Sat). **Requires ≥1 participant** — with no `--user`/`--team` an interactive run prompts (default team suggested); non-interactive errors. | diff --git a/tests/authoring_helpers_test.py b/tests/authoring_helpers_test.py index c5c96f8..b6455e8 100644 --- a/tests/authoring_helpers_test.py +++ b/tests/authoring_helpers_test.py @@ -9,6 +9,7 @@ from dailybot_cli import display from dailybot_cli.commands.authoring_helpers import ( + FORM_QUESTION_TYPES, MAX_QUESTIONS, AuthoringError, build_checkin_config, @@ -77,6 +78,48 @@ def test_is_blocker_flag_forwarded(self) -> None: assert build_question("boolean", "Blocked?", is_blocker=True)["is_blocker"] is True +class TestFileQuestions: + """Tables (forms) take a ``file`` question; check-ins don't (server refuses it).""" + + def test_file_is_a_form_type(self) -> None: + assert "file" in FORM_QUESTION_TYPES + + def test_file_question_for_a_form(self) -> None: + result: dict[str, Any] = build_question( + "file", "Screenshot", required=False, allowed_types=FORM_QUESTION_TYPES + ) + assert result == { + "question_type": "file", + "question": "Screenshot", + "required": False, + "is_blocker": False, + } + + def test_file_rejected_by_default_for_check_ins(self) -> None: + with pytest.raises(AuthoringError, match="Invalid question type 'file'"): + build_question("file", "Screenshot") + + def test_file_cannot_be_the_blocker(self) -> None: + with pytest.raises(AuthoringError, match="blocker"): + build_question("file", "Screenshot", is_blocker=True, allowed_types=FORM_QUESTION_TYPES) + + def test_file_takes_no_options(self) -> None: + with pytest.raises(AuthoringError): + build_question("file", "Screenshot", options=["a"], allowed_types=FORM_QUESTION_TYPES) + + def test_questions_file_accepts_file_for_forms(self, tmp_path: Path) -> None: + path = tmp_path / "q.json" + path.write_text(json.dumps([{"type": "file", "label": "Screenshot", "required": False}])) + questions = parse_questions_file(str(path), allowed_types=FORM_QUESTION_TYPES) + assert questions[0]["question_type"] == "file" + + def test_questions_file_rejects_file_for_check_ins(self, tmp_path: Path) -> None: + path = tmp_path / "q.json" + path.write_text(json.dumps([{"type": "file", "label": "Screenshot"}])) + with pytest.raises(AuthoringError, match="Invalid question type 'file'"): + parse_questions_file(str(path)) + + class TestBuildQuestionEditFields: def test_empty_when_nothing_provided(self) -> None: assert build_question_edit_fields(None, None, None, None) == {} diff --git a/tests/checkin_authoring_test.py b/tests/checkin_authoring_test.py index f1cfa2d..39d89f9 100644 --- a/tests/checkin_authoring_test.py +++ b/tests/checkin_authoring_test.py @@ -459,6 +459,27 @@ def test_add_jump_to_without_value_fails_fast(self, runner: CliRunner) -> None: assert result.exit_code != 0 assert "--jump-if-equals" in result.output + def test_add_file_is_rejected_for_check_ins(self, runner: CliRunner) -> None: + # File questions are for tables; a check-in can't ask for an upload. + with _auth(), _client() as cls: + result = runner.invoke( + cli, + [ + "checkin", + "questions", + "add", + "fu-1", + "--type", + "file", + "--question", + "Screenshot", + "--ai-short-question", + ], + ) + cls.return_value.add_checkin_question.assert_not_called() + assert result.exit_code != 0 + assert "Invalid question type 'file'" in result.output + def test_add_without_short_question_is_rejected(self, runner: CliRunner) -> None: # A report title is required unless --ai-short-question opts into AI titling. with _auth(), _client() as cls: diff --git a/tests/form_authoring_test.py b/tests/form_authoring_test.py index badf35d..529f3a8 100644 --- a/tests/form_authoring_test.py +++ b/tests/form_authoring_test.py @@ -470,6 +470,83 @@ def test_add_with_blocker(self, runner: CliRunner) -> None: assert result.exit_code == 0 assert client.add_form_question.call_args[0][1]["is_blocker"] is True + def test_add_file(self, runner: CliRunner) -> None: + """Tables take a File question (e.g. an optional Screenshot).""" + with _auth(), _client() as cls: + client: MagicMock = cls.return_value + client.add_form_question.return_value = {"uuid": "q-file", "question": "Screenshot"} + result = runner.invoke( + cli, + [ + "form", + "questions", + "add", + "form-uuid", + "--type", + "file", + "--question", + "Screenshot", + "--short-question", + "Screenshot", + "--optional", + ], + ) + assert result.exit_code == 0, result.output + payload = client.add_form_question.call_args[0][1] + assert payload["question_type"] == "file" + assert payload["required"] is False + + def test_add_file_as_blocker_fails_fast(self, runner: CliRunner) -> None: + with _auth(), _client() as cls: + result = runner.invoke( + cli, + [ + "form", + "questions", + "add", + "form-uuid", + "--type", + "file", + "--question", + "Screenshot", + "--blocker", + "--ai-short-question", + ], + ) + cls.return_value.add_form_question.assert_not_called() + assert result.exit_code != 0 + assert "blocker" in result.output + + def test_add_help_lists_file(self, runner: CliRunner) -> None: + result = runner.invoke(cli, ["form", "questions", "add", "--help"]) + assert result.exit_code == 0 + assert "file" in result.output + + def test_create_with_a_file_question(self, runner: CliRunner, tmp_path: Any) -> None: + qfile = tmp_path / "q.json" + qfile.write_text( + json.dumps( + [ + {"question_type": "text", "question": "What?", "short_question": "What"}, + { + "question_type": "file", + "question": "Screenshot", + "short_question": "Screenshot", + "required": False, + }, + ] + ) + ) + with _auth(), _client() as cls: + client: MagicMock = cls.return_value + client.create_form.return_value = FORM_PAYLOAD + result = runner.invoke( + cli, ["form", "create", "-n", "Bugs", "--questions-file", str(qfile)] + ) + assert result.exit_code == 0, result.output + sent = json.dumps(client.create_form.call_args) + assert '"question_type": "file"' in sent + def test_add_multiple_choice_needs_options(self, runner: CliRunner) -> None: with _auth(), _client() as cls: result = runner.invoke(