Coverage for /home/runner/work/bijux-cli/bijux-cli/src/bijux_cli/commands/memory/clear.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-19 23:36 +0000

1# SPDX-License-Identifier: MIT 

2# Copyright © 2025 Bijan Mousavi 

3 

4"""Implements the `memory clear` subcommand for the Bijux CLI. 

5 

6This module contains the logic for permanently erasing all entries from the 

7transient, in-memory data store. This action is irreversible for the current 

8process. A structured confirmation is emitted upon success. 

9 

10Output Contract: 

11 * Success: `{"status": "cleared", "count": 0}` 

12 * Verbose: Adds `{"python": str, "platform": str}` to the payload. 

13 * Error: `{"error": str, "code": int}` 

14 

15Exit Codes: 

16 * `0`: Success. 

17 * `1`: An unexpected error occurred (e.g., service unavailable, clear failed). 

18""" 

19 

20from __future__ import annotations 

21 

22from collections.abc import Mapping 

23import platform 

24 

25import typer 

26 

27from bijux_cli.commands.memory.utils import resolve_memory_service 

28from bijux_cli.commands.utilities import ( 

29 ascii_safe, 

30 emit_error_and_exit, 

31 new_run_command, 

32 validate_common_flags, 

33) 

34from bijux_cli.core.constants import ( 

35 HELP_DEBUG, 

36 HELP_FORMAT, 

37 HELP_NO_PRETTY, 

38 HELP_QUIET, 

39 HELP_VERBOSE, 

40) 

41 

42 

43def _build_payload(include_runtime: bool) -> Mapping[str, object]: 

44 """Builds the payload confirming that the in-memory store was cleared. 

45 

46 Args: 

47 include_runtime (bool): If True, includes Python and platform info. 

48 

49 Returns: 

50 Mapping[str, object]: A dictionary containing the status, a count of 0, 

51 and optional runtime metadata. 

52 """ 

53 payload: dict[str, object] = {"status": "cleared", "count": 0} 

54 if include_runtime: 

55 payload["python"] = ascii_safe(platform.python_version(), "python_version") 

56 payload["platform"] = ascii_safe(platform.platform(), "platform") 

57 return payload 

58 

59 

60def clear_memory( 

61 quiet: bool = typer.Option(False, "-q", "--quiet", help=HELP_QUIET), 

62 verbose: bool = typer.Option(False, "-v", "--verbose", help=HELP_VERBOSE), 

63 fmt: str = typer.Option("json", "-f", "--format", help=HELP_FORMAT), 

64 pretty: bool = typer.Option(True, "--pretty/--no-pretty", help=HELP_NO_PRETTY), 

65 debug: bool = typer.Option(False, "-d", "--debug", help=HELP_DEBUG), 

66) -> None: 

67 """Removes all key-value pairs from the transient in-memory store. 

68 

69 This command erases all entries from the memory service and emits a 

70 structured payload to confirm the operation. 

71 

72 Args: 

73 quiet (bool): If True, suppresses all output except for errors. 

74 verbose (bool): If True, includes Python/platform details in the output. 

75 fmt (str): The output format, "json" or "yaml". 

76 pretty (bool): If True, pretty-prints the output. 

77 debug (bool): If True, enables debug diagnostics. 

78 

79 Returns: 

80 None: 

81 

82 Raises: 

83 SystemExit: Always exits with a contract-compliant status code and 

84 payload, indicating success or detailing an error. 

85 """ 

86 command = "memory clear" 

87 

88 fmt_lower = validate_common_flags(fmt, command, quiet) 

89 

90 memory_svc = resolve_memory_service(command, fmt_lower, quiet, verbose, debug) 

91 

92 try: 

93 memory_svc.clear() 

94 except Exception as exc: 

95 emit_error_and_exit( 

96 f"Failed to clear memory: {exc}", 

97 code=1, 

98 failure="clear_failed", 

99 command=command, 

100 fmt=fmt_lower, 

101 quiet=quiet, 

102 include_runtime=verbose, 

103 debug=debug, 

104 ) 

105 

106 new_run_command( 

107 command_name=command, 

108 payload_builder=lambda include: _build_payload(include), 

109 quiet=quiet, 

110 verbose=verbose, 

111 fmt=fmt_lower, 

112 pretty=pretty, 

113 debug=debug, 

114 )