Files
amcs/internal/tools/files_test.go
Hein f41c512f36 test(tools): add unit tests for error handling functions
* Implement tests for error functions like errRequiredField, errInvalidField, and errEntityNotFound.
* Ensure proper metadata is returned for various error scenarios.
* Validate error handling in CRM, Files, and other tools.
* Introduce tests for parsing stored file IDs and UUIDs.
* Enhance coverage for helper functions related to project resolution and session management.
2026-03-31 15:10:07 +02:00

76 lines
1.9 KiB
Go

package tools
import (
"testing"
"github.com/google/uuid"
)
func TestDecodeBase64AcceptsWhitespaceAndMultipleVariants(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{name: "standard with whitespace", input: "aG V s\nbG8=", want: "hello"},
{name: "raw standard", input: "aGVsbG8", want: "hello"},
{name: "standard with extra padding", input: "aGVsbG8==", want: "hello"},
{name: "standard url-safe payload", input: "--8=", want: string([]byte{0xfb, 0xef})},
{name: "raw url-safe payload", input: "--8", want: string([]byte{0xfb, 0xef})},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := decodeBase64(tc.input)
if err != nil {
t.Fatalf("decodeBase64(%q) error = %v", tc.input, err)
}
if string(got) != tc.want {
t.Fatalf("decodeBase64(%q) = %q, want %q", tc.input, string(got), tc.want)
}
})
}
}
func TestParseStoredFileIDAcceptsUUIDAndURI(t *testing.T) {
id := uuid.New()
tests := []struct {
name string
input string
want uuid.UUID
}{
{name: "bare uuid", input: id.String(), want: id},
{name: "resource uri", input: fileURIPrefix + id.String(), want: id},
{name: "resource uri with surrounding whitespace", input: " " + fileURIPrefix + id.String() + " ", want: id},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := parseStoredFileID(tc.input)
if err != nil {
t.Fatalf("parseStoredFileID(%q) error = %v", tc.input, err)
}
if got != tc.want {
t.Fatalf("parseStoredFileID(%q) = %v, want %v", tc.input, got, tc.want)
}
})
}
}
func TestParseStoredFileIDRejectsInvalidValues(t *testing.T) {
tests := []string{
"",
"not-a-uuid",
fileURIPrefix + "not-a-uuid",
}
for _, input := range tests {
t.Run(input, func(t *testing.T) {
if _, err := parseStoredFileID(input); err == nil {
t.Fatalf("parseStoredFileID(%q) = nil error, want error", input)
}
})
}
}