// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build !ts_omit_drive && ts_mac_gui

package cli

import (
	"bytes"
	"context"
	"flag"
	"strings"
	"testing"

	"github.com/peterbourgon/ff/v3/ffcli"
)

// In macOS GUI builds, the `drive` command should not appear in
// the help text generated by ffcli.
func TestDriveCommandHiddenInHelpText(t *testing.T) {
	root := newRootCmd()

	var buf bytes.Buffer
	root.FlagSet = flag.NewFlagSet("tailscale", flag.ContinueOnError)
	root.FlagSet.SetOutput(&buf)

	ffcli.DefaultUsageFunc(root)

	output := buf.String()

	if strings.Contains(output, "drive") {
		t.Errorf("found hidden command 'drive' in help output:\n%q", output)
	}
}

// Running the drive command always prints an error pointing you to
// the GUI app, regardless of input.
func TestDriveCommandPrintsError(t *testing.T) {
	commands := [][]string{
		{"drive"},
		{"drive", "share", "myfile.txt", "/path/to/myfile.txt"},
		{"drive", "rename", "oldname.txt", "newname.txt"},
		{"drive", "unshare", "myfile.txt"},
		{"drive", "list"},
	}

	for _, args := range commands {
		root := newRootCmd()

		if err := root.Parse(args); err != nil {
			t.Errorf("unable to parse args %q, got err %v", args, err)
			continue
		}

		t.Logf("running `tailscale drive %q`", strings.Join(args, " "))
		err := root.Run(context.Background())
		if err == nil {
			t.Error("expected error, but got nil", args)
		}

		expectedText := "Taildrive CLI commands are not supported when using the macOS GUI app."
		if !strings.Contains(err.Error(), expectedText) {
			t.Errorf("error was not expected: want %q, got %q", expectedText, err.Error())
		}
	}
}
