package persistence

import (
	"bytes"
	"strings"
	"testing"
)

func TestRuntimePlatformCommandResultEncodingPreservesOpaqueBytes(t *testing.T) {
	input := []byte{0xff, 0x00, 0xfe, '{', '}'}
	encoded := encodeRuntimePlatformCommandResult(input)
	if !strings.HasPrefix(encoded, runtimePlatformCommandResultPrefix) {
		t.Fatalf("encoded result = %q, want explicit base64 prefix", encoded)
	}
	decoded, err := decodeRuntimePlatformCommandResult(encoded)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(decoded, input) {
		t.Fatalf("decoded result = %x, want %x", decoded, input)
	}
	for _, invalid := range []string{"legacy raw result", runtimePlatformCommandResultPrefix, runtimePlatformCommandResultPrefix + "%%%"} {
		if _, err := decodeRuntimePlatformCommandResult(invalid); err == nil {
			t.Fatalf("decodeRuntimePlatformCommandResult(%q) accepted invalid encoding", invalid)
		}
	}
}

func mustRuntimeWorkScopeDigest(t *testing.T, input RuntimePlatformCommandAcceptance) string {
	t.Helper()
	digest, err := (RuntimeWorkScope{
		OrgID:               input.OrgID,
		AppInstallationID:   input.AppInstallationID,
		RuntimeCapabilityID: input.RuntimeCapabilityID,
	}).Digest()
	if err != nil {
		t.Fatal(err)
	}
	return digest
}
