package main

import "testing"

func TestParseInteractiveCommand(t *testing.T) {
	tests := []struct {
		name    string
		line    string
		want    interactiveCommand
		wantErr string
	}{
		{
			name: "text command preserves spaces in body",
			line: "text peer-b hello over the same connection",
			want: interactiveCommand{
				name:  interactiveCommandText,
				to:    "peer-b",
				value: "hello over the same connection",
			},
		},
		{
			name: "file command preserves spaces in path",
			line: "file peer-b /tmp/demo payload.bin",
			want: interactiveCommand{
				name:  interactiveCommandFile,
				to:    "peer-b",
				value: "/tmp/demo payload.bin",
			},
		},
		{
			name: "help alias",
			line: "?",
			want: interactiveCommand{
				name: interactiveCommandHelp,
			},
		},
		{
			name: "quit alias",
			line: "exit",
			want: interactiveCommand{
				name: interactiveCommandQuit,
			},
		},
		{
			name:    "empty command",
			line:    "   ",
			wantErr: errEmptyInteractiveCommand.Error(),
		},
		{
			name:    "text requires payload",
			line:    "text peer-b",
			wantErr: "text command requires a non-empty payload",
		},
		{
			name:    "file requires target and payload",
			line:    "file",
			wantErr: "file command requires a target peer and payload",
		},
		{
			name:    "unknown command",
			line:    "ping peer-b",
			wantErr: `unknown command "ping"; type help for usage`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := parseInteractiveCommand(tt.line)
			if tt.wantErr != "" {
				if err == nil {
					t.Fatalf("parseInteractiveCommand(%q) error = nil, want %q", tt.line, tt.wantErr)
				}
				if err.Error() != tt.wantErr {
					t.Fatalf("parseInteractiveCommand(%q) error = %q, want %q", tt.line, err.Error(), tt.wantErr)
				}
				return
			}

			if err != nil {
				t.Fatalf("parseInteractiveCommand(%q) error = %v", tt.line, err)
			}
			if got != tt.want {
				t.Fatalf("parseInteractiveCommand(%q) = %+v, want %+v", tt.line, got, tt.want)
			}
		})
	}
}
