// Copyright [2026] [Argus]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build unit

package test

import (
	"fmt"
	"testing"
)

func TestMust(t *testing.T) {
	tests := []struct {
		name  string
		fn    func() (int, error)
		panic bool
		want  int
	}{
		{
			name: "no error",
			fn: func() (int, error) {
				return 42, nil
			},
			want: 42,
		},
		{
			name: "with error",
			fn: func() (int, error) {
				return 6, fmt.Errorf("some error")
			},
			panic: true,
			want:  0,
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {

			prefix := fmt.Sprintf("%s\nMust()", packageName)

			if tc.panic {
				defer func() {
					if r := recover(); r == nil {
						t.Fatalf("%s the embedded function did not panic", prefix)
					}
				}()
			}

			// WHEN: Must is called.
			result := Must(t, tc.fn)

			// THEN: the result should be the expected value.
			if result != tc.want {
				t.Errorf(
					"%s mismatch\ngot:  %d\nwant: %d",
					prefix, result, tc.want,
				)
			}
		})
	}
}
