From 7b9c9647ac0101b88aba4fe2cb604f01f4c9b456 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Sun, 7 Jun 2026 16:41:47 +0800 Subject: [PATCH] test: add TestCallPluginFunction + final run - all 13 tests pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TestCallPluginFunction: verifies DoString calls Lua functions correctly - add(3,4) → 7, greet('World') → 'Hello, World', get_table() → table - Full suite: 13 tests, 0 failures - go build ./... clean --- internal/core/plugins/runtime_test.go | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/internal/core/plugins/runtime_test.go b/internal/core/plugins/runtime_test.go index 876f725..2bbde0d 100644 --- a/internal/core/plugins/runtime_test.go +++ b/internal/core/plugins/runtime_test.go @@ -390,3 +390,86 @@ func copyDir(src, dst string) error { return os.WriteFile(target, data, info.Mode()) }) } + +// TestCallPluginFunction verifies that DoString can call Lua functions +// and return results through the VM. +func TestCallPluginFunction(t *testing.T) { + dir := t.TempDir() + + // Create a simple plugin with a global function + pluginDir := filepath.Join(dir, "testfunc") + if err := os.MkdirAll(pluginDir, 0755); err != nil { + t.Fatal(err) + } + + luaCode := ` +function add(a, b) + return a + b +end + +function greet(name) + return "Hello, " .. name +end + +function get_table() + return { x = 42, y = "test" } +end +` + if err := os.WriteFile(filepath.Join(pluginDir, "main.lua"), []byte(luaCode), 0644); err != nil { + t.Fatal(err) + } + + p := &Plugin{ + Meta: Meta{ + Name: "testfunc", + Hooks: map[string]string{ + "on_install": "on_install", + }, + }, + Dir: pluginDir, + DataDir: filepath.Join(dir, "data"), + Active: true, + HasInstall: true, + } + os.MkdirAll(p.DataDir, 0755) + + vm, err := NewLuaVM(p) + if err != nil { + t.Fatalf("NewLuaVM: %v", err) + } + defer vm.Close() + + // Load main.lua + if err := vm.LoadScript("main.lua"); err != nil { + t.Fatalf("LoadScript: %v", err) + } + + // Test 1: add(3, 4) → "7" + result, err := vm.DoString("return add(3, 4)") + if err != nil { + t.Fatalf("DoString(add): %v", err) + } + if result != "7" { + t.Errorf("add(3,4) = %q, want %q", result, "7") + } + + // Test 2: greet("World") → "Hello, World" + result, err = vm.DoString("return greet('World')") + if err != nil { + t.Fatalf("DoString(greet): %v", err) + } + if result != "Hello, World" { + t.Errorf("greet('World') = %q, want %q", result, "Hello, World") + } + + // Test 3: get_table() → table string (just verify no error) + result, err = vm.DoString("return get_table()") + if err != nil { + t.Fatalf("DoString(get_table): %v", err) + } + if result == "" || result == "nil" { + t.Errorf("get_table() returned empty/nil: %q", result) + } + + t.Logf("DoString tests passed: add=%q, greet=%q, get_table=%q", result, result, result) +}