package store

import (
	"os"
	"path/filepath"
	"testing"
)

func TestContentStoreDeduplicatesByHash(t *testing.T) {
	root := t.TempDir()

	store, err := New(root)
	if err != nil {
		t.Fatalf("New() error = %v", err)
	}

	first, err := store.PutBytes([]byte("hello"))
	if err != nil {
		t.Fatalf("PutBytes() first error = %v", err)
	}

	second, err := store.PutBytes([]byte("hello"))
	if err != nil {
		t.Fatalf("PutBytes() second error = %v", err)
	}

	if first.Hash != second.Hash {
		t.Fatalf("hash mismatch: %s != %s", first.Hash, second.Hash)
	}

	if _, err := os.Stat(filepath.Join(root, first.Hash[0:2], first.Hash[2:4], first.Hash)); err != nil {
		t.Fatalf("expected content file to exist: %v", err)
	}
}
