package organ import ( "context" "os" "testing" "time" "git.mleku.dev/mleku/dendrite/pkg/ratio" ) func TestCompileOrgan(t *testing.T) { // Use GOROOT from env or default. goRoot := os.Getenv("GOROOT") if goRoot == "" { goRoot = "/home/mleku/sdk/go1.24.6" } // Verify the go binary exists. if _, err := os.Stat(goRoot + "/bin/go"); err != nil { t.Skipf("Go not found at %s: %v", goRoot, err) } source := `package main import "unsafe" var pool [1 << 16]byte var poolOffset uint32 //go:wasmexport alloc func alloc(size uint32) uint32 { if poolOffset+size > uint32(len(pool)) { poolOffset = 0 } ptr := poolOffset poolOffset += size return uint32(uintptr(unsafe.Pointer(&pool[ptr]))) } //go:wasmexport dealloc func dealloc(ptr uint32, size uint32) {} //go:wasmexport process func process(inputPtr uint32, inputLen uint32) uint64 { base := uint32(uintptr(unsafe.Pointer(&pool[0]))) offset := inputPtr - base if offset+inputLen > uint32(len(pool)) { return 0 } input := pool[offset : offset+inputLen] result := []byte("ok:") result = append(result, input...) outPtr := alloc(uint32(len(result))) outOffset := outPtr - base copy(pool[outOffset:], result) return uint64(outPtr)<<32 | uint64(len(result)) } func main() {} ` ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() wasmBytes, err := CompileOrgan(ctx, []byte(source), TypeEnzyme, goRoot, "") if err != nil { t.Fatalf("compile: %v", err) } if len(wasmBytes) == 0 { t.Fatal("compiled WASM is empty") } // Should be a valid WASM module (starts with \x00asm). if len(wasmBytes) < 4 || string(wasmBytes[:4]) != "\x00asm" { t.Fatal("compiled output is not a valid WASM module") } t.Logf("compiled WASM: %d bytes", len(wasmBytes)) // Load it into a registry and verify it works. reg, err := NewRegistry(ctx) if err != nil { t.Fatalf("new registry: %v", err) } defer reg.Close() manifest := Manifest{Type: TypeEnzyme, Version: 1, Fitness: ratio.Half} organ, err := reg.Load(wasmBytes, manifest) if err != nil { t.Fatalf("load compiled organ: %v", err) } if organ.Status != StatusLoaded { t.Errorf("expected StatusLoaded, got %d", organ.Status) } // Call it. result, err := reg.Call(organ.Manifest.ID, 0x01, []byte("test")) if err != nil { t.Fatalf("call: %v", err) } expected := "ok:\x01test" if string(result) != expected { t.Errorf("result: want %q, got %q", expected, string(result)) } }