package main

import (
	"context"
	"testing"
)

func TestCSVToASCIITable(t *testing.T) {
	filter := TextFilters["csv-to-ascii-table"].Filter

	tests := []struct {
		name    string
		input   string
		want    string
		wantErr bool
	}{
		{
			name: "basic table with quoted value",
			input: "alpha,bravo\n" +
				"1,b\n" +
				"\"long value\",something\n",
			want: "alpha      bravo\n" +
				"---------- ---------\n" +
				"1          b\n" +
				"long value something\n" +
				"---------- ---------\n",
		},
		{
			name:  "no trailing newline",
			input: "a,b\n1,2",
			want: "a b\n" +
				"- -\n" +
				"1 2\n" +
				"- -",
		},
		{
			name:  "quoted field containing comma",
			input: "name,desc\n\"x, y\",z\n",
			want: "name desc\n" +
				"---- ----\n" +
				"x, y z\n" +
				"---- ----\n",
		},
		{
			name:  "ragged rows padded with empty cells",
			input: "a,b,c\n1\n2,3\n",
			want: "a b c\n" +
				"- - -\n" +
				"1\n" +
				"2 3\n" +
				"- - -\n",
		},
		{
			name:  "header only",
			input: "one,two\n",
			want: "one two\n" +
				"--- ---\n" +
				"--- ---\n",
		},
		{
			name:  "empty input unchanged",
			input: "",
			want:  "",
		},
		{
			name:    "invalid CSV returns error",
			input:   "a,\"b\nc",
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resp, err := filter(context.Background(), tt.input)
			if tt.wantErr {
				if err == nil {
					t.Fatalf("expected error, got output %q", resp.Output)
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if resp.Output != tt.want {
				t.Errorf("output mismatch\ngot:\n%s\nwant:\n%s", resp.Output, tt.want)
			}
			if resp.Append {
				t.Errorf("Append should be false")
			}
		})
	}
}
