go/tests/embed_test.go
Snider 1ca010e1fb test: rewrite test suite for AX primitives API
164 tests, 41.3% coverage. Tests written against the public API only
(external test package, no _test.go in pkg/core/).

Covers: New(Options), Data, Drive, Config, Service, Error, IPC,
Fs, Cli, Lock, Array, Log, App, Runtime, Task.

Fixes: NewCommand now inits flagset, New() wires Cli root command.

Old tests removed — they referenced With*, RegisterService, and
other patterns that no longer exist.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-20 08:42:38 +00:00

132 lines
3.3 KiB
Go

package core_test
import (
"bytes"
"compress/gzip"
"encoding/base64"
"os"
"path/filepath"
"testing"
. "forge.lthn.ai/core/go/pkg/core"
"github.com/stretchr/testify/assert"
)
// --- Embed (Mount + ReadFile + Sub) ---
func TestMount_Good(t *testing.T) {
emb, err := Mount(testFS, "testdata")
assert.NoError(t, err)
assert.NotNil(t, emb)
}
func TestMount_Bad(t *testing.T) {
_, err := Mount(testFS, "nonexistent")
assert.Error(t, err)
}
func TestEmbed_ReadFile_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
data, err := emb.ReadFile("test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello from testdata\n", string(data))
}
func TestEmbed_ReadString_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
s, err := emb.ReadString("test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello from testdata\n", s)
}
func TestEmbed_Open_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
f, err := emb.Open("test.txt")
assert.NoError(t, err)
defer f.Close()
}
func TestEmbed_ReadDir_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
entries, err := emb.ReadDir(".")
assert.NoError(t, err)
assert.NotEmpty(t, entries)
}
func TestEmbed_Sub_Good(t *testing.T) {
emb, _ := Mount(testFS, ".")
sub, err := emb.Sub("testdata")
assert.NoError(t, err)
data, err := sub.ReadFile("test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello from testdata\n", string(data))
}
func TestEmbed_BaseDir_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
assert.Equal(t, "testdata", emb.BaseDir())
}
func TestEmbed_FS_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
assert.NotNil(t, emb.FS())
}
func TestEmbed_EmbedFS_Good(t *testing.T) {
emb, _ := Mount(testFS, "testdata")
efs := emb.EmbedFS()
// Should return the original embed.FS
_, err := efs.ReadFile("testdata/test.txt")
assert.NoError(t, err)
}
// --- Extract (Template Directory) ---
func TestExtract_Good(t *testing.T) {
dir := t.TempDir()
err := Extract(testFS, dir, nil)
assert.NoError(t, err)
// testdata/test.txt should be extracted
content, err := os.ReadFile(filepath.Join(dir, "testdata", "test.txt"))
assert.NoError(t, err)
assert.Equal(t, "hello from testdata\n", string(content))
}
// --- Asset Pack (Build-time) ---
func TestAddGetAsset_Good(t *testing.T) {
AddAsset("test-group", "greeting", mustCompress("hello world"))
result, err := GetAsset("test-group", "greeting")
assert.NoError(t, err)
assert.Equal(t, "hello world", result)
}
func TestGetAsset_Bad(t *testing.T) {
_, err := GetAsset("missing-group", "missing")
assert.Error(t, err)
AddAsset("exists", "item", mustCompress("data"))
_, err = GetAsset("exists", "missing-item")
assert.Error(t, err)
}
func TestGetAssetBytes_Good(t *testing.T) {
AddAsset("bytes-group", "file", mustCompress("binary content"))
data, err := GetAssetBytes("bytes-group", "file")
assert.NoError(t, err)
assert.Equal(t, []byte("binary content"), data)
}
// mustCompress is a test helper — compresses a string the way AddAsset expects.
func mustCompress(input string) string {
// AddAsset stores pre-compressed data. We need to compress it the same way.
// Use the internal format: base64(gzip(input))
var buf bytes.Buffer
b64 := base64.NewEncoder(base64.StdEncoding, &buf)
gz, _ := gzip.NewWriterLevel(b64, gzip.BestCompression)
gz.Write([]byte(input))
gz.Close()
b64.Close()
return buf.String()
}