New tests: Data List/ListNames/Extract, I18n with mock Translator, Fs full surface (EnsureDir, IsDir, IsFile, Exists, List, Stat, Open, Create, Append, ReadStream, WriteStream, Delete, DeleteAll, Rename), Log all levels + Security + Username + Default + LogErr + LogPan, Embed ScanAssets + GeneratePack + MountEmbed, Runtime ServiceName, Core LogError/LogWarn/Must helpers. Fixes: NewCommand inits flagset, New() wires Cli root command + app. Remaining 0% (excluding CLI/App): compress, getAllFiles (internal), Reports/appendReport (needs ErrorPanic filePath), SetOutput (trivial). Co-Authored-By: Virgil <virgil@lethean.io>
184 lines
3.9 KiB
Go
184 lines
3.9 KiB
Go
package core_test
|
|
|
|
import (
|
|
"embed"
|
|
"io"
|
|
"testing"
|
|
|
|
. "forge.lthn.ai/core/go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
//go:embed testdata
|
|
var testFS embed.FS
|
|
|
|
// --- Data (Embedded Content Mounts) ---
|
|
|
|
func TestData_New_Good(t *testing.T) {
|
|
c := New()
|
|
r := c.Data().New(Options{
|
|
{K: "name", V: "test"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
assert.True(t, r.OK)
|
|
assert.NotNil(t, r.Value)
|
|
}
|
|
|
|
func TestData_New_Bad(t *testing.T) {
|
|
c := New()
|
|
|
|
// Missing name
|
|
r := c.Data().New(Options{
|
|
{K: "source", V: testFS},
|
|
})
|
|
assert.False(t, r.OK)
|
|
|
|
// Missing source
|
|
r = c.Data().New(Options{
|
|
{K: "name", V: "test"},
|
|
})
|
|
assert.False(t, r.OK)
|
|
|
|
// Wrong source type
|
|
r = c.Data().New(Options{
|
|
{K: "name", V: "test"},
|
|
{K: "source", V: "not-an-fs"},
|
|
})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestData_ReadString_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
content, err := c.Data().ReadString("app/test.txt")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hello from testdata\n", content)
|
|
}
|
|
|
|
func TestData_ReadString_Bad(t *testing.T) {
|
|
c := New()
|
|
_, err := c.Data().ReadString("nonexistent/file.txt")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestData_ReadFile_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
data, err := c.Data().ReadFile("app/test.txt")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hello from testdata\n", string(data))
|
|
}
|
|
|
|
func TestData_Get_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "brain"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
emb := c.Data().Get("brain")
|
|
assert.NotNil(t, emb)
|
|
|
|
// Read via the Embed directly
|
|
file, err := emb.Open("test.txt")
|
|
assert.NoError(t, err)
|
|
defer file.Close()
|
|
content, _ := io.ReadAll(file)
|
|
assert.Equal(t, "hello from testdata\n", string(content))
|
|
}
|
|
|
|
func TestData_Get_Bad(t *testing.T) {
|
|
c := New()
|
|
emb := c.Data().Get("nonexistent")
|
|
assert.Nil(t, emb)
|
|
}
|
|
|
|
func TestData_Mounts_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{{K: "name", V: "a"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
c.Data().New(Options{{K: "name", V: "b"}, {K: "source", V: testFS}, {K: "path", V: "testdata"}})
|
|
mounts := c.Data().Mounts()
|
|
assert.Len(t, mounts, 2)
|
|
assert.Contains(t, mounts, "a")
|
|
assert.Contains(t, mounts, "b")
|
|
}
|
|
|
|
// --- Legacy Embed() accessor ---
|
|
|
|
func TestEmbed_Legacy_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "testdata"},
|
|
})
|
|
// Legacy accessor reads from Data mount "app"
|
|
emb := c.Embed()
|
|
assert.NotNil(t, emb)
|
|
}
|
|
|
|
// --- Data List / ListNames ---
|
|
|
|
func TestData_List_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "."},
|
|
})
|
|
entries, err := c.Data().List("app/testdata")
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, entries)
|
|
}
|
|
|
|
func TestData_List_Bad(t *testing.T) {
|
|
c := New()
|
|
_, err := c.Data().List("nonexistent/path")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestData_ListNames_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "."},
|
|
})
|
|
names, err := c.Data().ListNames("app/testdata")
|
|
assert.NoError(t, err)
|
|
assert.Contains(t, names, "test")
|
|
}
|
|
|
|
// --- Data Extract ---
|
|
|
|
func TestData_Extract_Good(t *testing.T) {
|
|
c := New()
|
|
c.Data().New(Options{
|
|
{K: "name", V: "app"},
|
|
{K: "source", V: testFS},
|
|
{K: "path", V: "."},
|
|
})
|
|
dir := t.TempDir()
|
|
err := c.Data().Extract("app/testdata", dir, nil)
|
|
assert.NoError(t, err)
|
|
|
|
// Verify extracted file
|
|
content, err := c.Fs().Read(dir + "/test.txt")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hello from testdata\n", content)
|
|
}
|
|
|
|
func TestData_Extract_Bad(t *testing.T) {
|
|
c := New()
|
|
err := c.Data().Extract("nonexistent/path", t.TempDir(), nil)
|
|
assert.Error(t, err)
|
|
}
|