gitgram/aitimemachine/beads/cmd/bd/readonly_test.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

158 lines
4.2 KiB
Go

package main
import (
"bytes"
"strings"
"testing"
)
// TestReadonlyModeBlocksWrites verifies that --readonly blocks write operations
func TestReadonlyModeBlocksWrites(t *testing.T) {
// Store original state
originalMode := readonlyMode
defer func() { readonlyMode = originalMode }()
// Enable readonly mode
readonlyMode = true
// Test that CheckReadonly exits with error
tests := []struct {
operation string
}{
{"create"},
{"update"},
{"close"},
{"delete"},
{"sync"},
{"import"},
{"reopen"},
{"edit"},
{"comment add"},
{"dep add"},
{"dep remove"},
{"label add"},
{"label remove"},
{"repair-deps"},
{"compact"},
{"duplicates --auto-merge"},
{"epic close-eligible"},
{"migrate"},
{"migrate-issues"},
{"rename-prefix"},
{"validate --fix-all"},
{"jira sync"},
}
for _, tc := range tests {
t.Run(tc.operation, func(t *testing.T) {
// CheckReadonly calls FatalError which calls os.Exit
// We can't test that directly, but we can verify the logic
if !readonlyMode {
t.Error("readonly mode should be enabled")
}
})
}
}
// TestReadonlyModeAllowsReads verifies that --readonly allows read operations
func TestReadonlyModeAllowsReads(t *testing.T) {
// Store original state
originalMode := readonlyMode
defer func() { readonlyMode = originalMode }()
// Enable readonly mode
readonlyMode = true
// Read operations should work - just verify the flag doesn't affect them
// by ensuring readonlyMode doesn't break anything
if !readonlyMode {
t.Error("readonly mode should be enabled")
}
// The actual read commands (list, show, ready) don't call CheckReadonly
// so they should work fine. This is verified by integration tests.
}
// TestCheckReadonlyReturnsEarlyWhenDisabled verifies CheckReadonly is a no-op when disabled
func TestCheckReadonlyReturnsEarlyWhenDisabled(t *testing.T) {
// Store original state
originalMode := readonlyMode
defer func() { readonlyMode = originalMode }()
// Disable readonly mode
readonlyMode = false
// Capture that CheckReadonly doesn't call FatalError
// Since FatalError calls os.Exit, we verify by ensuring we don't panic/exit
// The function should just return early
// This test passes if it completes without calling os.Exit
// Since we can't easily mock os.Exit, we just verify the logic
if readonlyMode {
t.Error("readonly mode should be disabled")
}
}
// TestReadonlyFlagRegistered verifies the --readonly flag is registered
func TestReadonlyFlagRegistered(t *testing.T) {
// Create a new root command to test flag registration
cmd := rootCmd
flag := cmd.PersistentFlags().Lookup("readonly")
if flag == nil {
t.Error("--readonly flag should be registered")
}
if flag != nil && flag.Usage == "" {
t.Error("--readonly flag should have usage text")
}
}
// TestReadonlyModeVariable ensures the variable exists and is accessible
func TestReadonlyModeVariable(t *testing.T) {
// Just verify the variable is accessible
_ = readonlyMode
// Set and unset to verify it's writable
original := readonlyMode
readonlyMode = true
if !readonlyMode {
t.Error("should be able to set readonlyMode to true")
}
readonlyMode = false
if readonlyMode {
t.Error("should be able to set readonlyMode to false")
}
readonlyMode = original
}
// TestCheckReadonlyErrorMessage verifies the error message format
func TestCheckReadonlyErrorMessage(t *testing.T) {
// The error message should mention the operation and readonly mode
expectedSubstrings := []string{"operation", "is not allowed", "read-only mode"}
// We can't easily test FatalError output, but we can verify the format
// by checking what error message CheckReadonly would produce
operation := "test-operation"
expectedMsg := "operation 'test-operation' is not allowed in read-only mode"
for _, substr := range expectedSubstrings {
if !strings.Contains(expectedMsg, substr) {
t.Errorf("error message should contain %q", substr)
}
}
// Verify operation name is included
if !strings.Contains(expectedMsg, operation) {
t.Errorf("error message should contain operation name %q", operation)
}
}
// capture is a helper to capture stdout/stderr (not currently used but available)
type capture struct {
buf *bytes.Buffer
}
func (c *capture) Write(p []byte) (n int, err error) {
return c.buf.Write(p)
}