Files
nidus/tools/nidusctl/main_test.go
T
2026-08-30 19:54:34 +02:00

177 lines
4.7 KiB
Go

package main
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
"testing"
"git.arnef.de/arnef/nidus/internal/db"
)
func runCLI(t *testing.T, args ...string) (int, string) {
t.Helper()
oldStdout, oldStderr := os.Stdout, os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe: %v", err)
}
os.Stdout = w
os.Stderr = w
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
}()
code := run(args)
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return code, buf.String()
}
func TestCalendarShareUnshareLifecycle(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "calendar", "share", "alice", "work", "bob", "write")
if code != 0 {
t.Fatalf("share exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "shared") {
t.Errorf("output = %q, want to contain 'shared'", out)
}
code, out = runCLI(t, "calendar", "shares", "alice", "work")
if code != 0 {
t.Fatalf("shares exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "bob") || !strings.Contains(out, "write") {
t.Errorf("output = %q, want to contain bob/write", out)
}
code, out = runCLI(t, "calendar", "unshare", "alice", "work", "bob")
if code != 0 {
t.Fatalf("unshare exit code = %d, output: %s", code, out)
}
code, out = runCLI(t, "calendar", "shares", "alice", "work")
if code != 0 {
t.Fatalf("shares (after unshare) exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "not shared with anyone") {
t.Errorf("output = %q, want 'not shared with anyone'", out)
}
}
func TestCalendarShareInvalidPermission(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "calendar", "share", "alice", "work", "bob", "admin")
if code != 2 {
t.Errorf("exit code = %d, want 2; output: %s", code, out)
}
if !strings.Contains(out, "invalid permission") {
t.Errorf("output = %q, want 'invalid permission'", out)
}
}
func TestCalendarUnshareNotFound(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "calendar", "unshare", "alice", "ghost", "bob")
if code != 1 {
t.Errorf("exit code = %d, want 1; output: %s", code, out)
}
if !strings.Contains(out, "not found") {
t.Errorf("output = %q, want 'not found'", out)
}
}
func TestAddressBookShareUnshareLifecycle(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "addressbook", "share", "alice", "contacts", "bob", "read")
if code != 0 {
t.Fatalf("share exit code = %d, output: %s", code, out)
}
code, out = runCLI(t, "addressbook", "shares", "alice", "contacts")
if code != 0 {
t.Fatalf("shares exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "bob") || !strings.Contains(out, "read") {
t.Errorf("output = %q, want to contain bob/read", out)
}
code, out = runCLI(t, "addressbook", "unshare", "alice", "contacts", "bob")
if code != 0 {
t.Fatalf("unshare exit code = %d, output: %s", code, out)
}
}
func TestUnknownUserWarningDoesNotBlockShare(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "calendar", "share", "alice", "work", "carol", "read")
if code != 0 {
t.Fatalf("exit code = %d, output: %s", code, out)
}
if !strings.Contains(out, "warning") || !strings.Contains(out, "carol") {
t.Errorf("output = %q, want a warning about unknown user carol", out)
}
if !strings.Contains(out, "shared") {
t.Errorf("output = %q, want the share to still succeed", out)
}
}
func TestNoArgsShowsUsage(t *testing.T) {
// No config needed since usage() is printed before config.Load for
// missing subcommands.
code, out := runCLI(t)
if code != 2 {
t.Errorf("exit code = %d, want 2", code)
}
if !strings.Contains(out, "Usage") {
t.Errorf("output = %q, want usage text", out)
}
}
func TestUnknownCommand(t *testing.T) {
dir := t.TempDir()
t.Setenv("NIDUS_DATA_DIR", filepath.Join(dir, "data"))
code, out := runCLI(t, "bogus")
if code != 2 {
t.Errorf("exit code = %d, want 2", code)
}
if !strings.Contains(out, "unknown command") {
t.Errorf("output = %q, want 'unknown command'", out)
}
}
// Sanity check that the CLI and internal/db agree on ErrShareNotFound
// being surfaced (not swallowed) through the exit code / error message.
func TestUnshareErrorIsShareNotFound(t *testing.T) {
dir := t.TempDir()
dbase, err := db.Open(filepath.Join(dir, "test.db"))
if err != nil {
t.Fatalf("db.Open: %v", err)
}
defer dbase.Close()
err = dbase.UnshareCalendar("alice", "work", "bob")
if !errors.Is(err, db.ErrShareNotFound) {
t.Errorf("err = %v, want ErrShareNotFound", err)
}
}