package database

import (
	"testing"

	kv "git.sr.ht/~ashkeel/kilovolt/v12"
)

func CreateInMemoryLocalClient(t *testing.T) (*LocalDBClient, kv.Driver) {
	// Create in-memory store and hub
	inMemoryStore := kv.MakeBackend()
	hub, err := kv.NewHub(inMemoryStore, kv.HubOptions{})
	if err != nil {
		t.Fatal(err)
	}
	go hub.Run()

	// Create local client
	client, err := NewLocalClient(hub)
	if err != nil {
		t.Fatal(err)
	}

	return client, inMemoryStore
}

func CleanupLocalClient(client *LocalDBClient) {
	if client.hub != nil {
		_ = client.Close()
		client.hub.Close()
	}
}
