|
| 1 | +package embed |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// payMCPFiles are the files the embedded pay_mcp plugin must ship. The plugin |
| 11 | +// loads from a directory, so plugin.yaml + __init__.py are load-critical; the |
| 12 | +// rest are imported relatively by __init__/register(). |
| 13 | +var payMCPFiles = []string{ |
| 14 | + "plugin.yaml", "__init__.py", "x402.py", "rails.py", "payment.py", "recovery.py", |
| 15 | +} |
| 16 | + |
| 17 | +func TestGetEmbeddedPluginNames(t *testing.T) { |
| 18 | + names, err := GetEmbeddedPluginNames() |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("unexpected error: %v", err) |
| 21 | + } |
| 22 | + found := false |
| 23 | + for _, n := range names { |
| 24 | + if n == "pay_mcp" { |
| 25 | + found = true |
| 26 | + } |
| 27 | + } |
| 28 | + if !found { |
| 29 | + t.Fatalf("embedded plugins %v missing pay_mcp", names) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestCopyPlugins_SeedsPayMCP(t *testing.T) { |
| 34 | + dst := t.TempDir() |
| 35 | + if err := CopyPlugins(dst); err != nil { |
| 36 | + t.Fatalf("CopyPlugins: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + for _, f := range payMCPFiles { |
| 40 | + p := filepath.Join(dst, "pay_mcp", f) |
| 41 | + info, err := os.Stat(p) |
| 42 | + if err != nil { |
| 43 | + t.Errorf("missing seeded file %s: %v", f, err) |
| 44 | + continue |
| 45 | + } |
| 46 | + if info.Size() == 0 { |
| 47 | + t.Errorf("seeded file %s is empty", f) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestCopyPlugins_NoPycache(t *testing.T) { |
| 53 | + dst := t.TempDir() |
| 54 | + if err := CopyPlugins(dst); err != nil { |
| 55 | + t.Fatalf("CopyPlugins: %v", err) |
| 56 | + } |
| 57 | + err := filepath.Walk(dst, func(path string, info os.FileInfo, err error) error { |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if info.IsDir() && info.Name() == "__pycache__" { |
| 62 | + t.Errorf("__pycache__ leaked into seed: %s", path) |
| 63 | + } |
| 64 | + if !info.IsDir() && (strings.HasSuffix(path, ".pyc") || strings.HasSuffix(path, ".pyo")) { |
| 65 | + t.Errorf("compiled python leaked into seed: %s", path) |
| 66 | + } |
| 67 | + return nil |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("walk: %v", err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// TestCopyPlugins_RelativeImportsOnly is the inject-by-default invariant: a |
| 75 | +// user-dir plugin is loaded under the synthetic package name |
| 76 | +// hermes_plugins.pay_mcp, and a stock hermes image has no bundled |
| 77 | +// plugins.pay_mcp to satisfy an absolute self-import. So the embedded copy must |
| 78 | +// import its own modules relatively (`from . import x402`), never |
| 79 | +// `from plugins.pay_mcp import x402`. Upstream locks this with |
| 80 | +// tests/plugins/test_pay_mcp_userdir_load.py; we re-assert on the vendored copy |
| 81 | +// because the two are synced by hand. |
| 82 | +func TestCopyPlugins_RelativeImportsOnly(t *testing.T) { |
| 83 | + dst := t.TempDir() |
| 84 | + if err := CopyPlugins(dst); err != nil { |
| 85 | + t.Fatalf("CopyPlugins: %v", err) |
| 86 | + } |
| 87 | + pyFiles := []string{"__init__.py", "x402.py", "rails.py", "payment.py", "recovery.py"} |
| 88 | + for _, f := range pyFiles { |
| 89 | + data, err := os.ReadFile(filepath.Join(dst, "pay_mcp", f)) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("read %s: %v", f, err) |
| 92 | + } |
| 93 | + if strings.Contains(string(data), "from plugins.pay_mcp") || |
| 94 | + strings.Contains(string(data), "import plugins.pay_mcp") { |
| 95 | + t.Errorf("%s uses an absolute self-import; must be relative "+ |
| 96 | + "(breaks load from the user-plugins dir on a stock image)", f) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestCopyPlugins_ManifestNamesPayMCP(t *testing.T) { |
| 102 | + dst := t.TempDir() |
| 103 | + if err := CopyPlugins(dst); err != nil { |
| 104 | + t.Fatalf("CopyPlugins: %v", err) |
| 105 | + } |
| 106 | + data, err := os.ReadFile(filepath.Join(dst, "pay_mcp", "plugin.yaml")) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("read plugin.yaml: %v", err) |
| 109 | + } |
| 110 | + // The seeded manifest name must match the plugins.enabled entry the agent |
| 111 | + // configs write (pay_mcp), or the plugin is discovered-but-not-enabled. |
| 112 | + if !strings.Contains(string(data), "name: pay_mcp") { |
| 113 | + t.Errorf("plugin.yaml does not declare `name: pay_mcp`:\n%s", data) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestCopyPlugins_PreservesUserPlugins mirrors the skills contract: re-seeding |
| 118 | +// must not delete a user-added plugin with a different name. |
| 119 | +func TestCopyPlugins_PreservesUserPlugins(t *testing.T) { |
| 120 | + dst := t.TempDir() |
| 121 | + custom := filepath.Join(dst, "my-own-plugin") |
| 122 | + if err := os.MkdirAll(custom, 0o755); err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + if err := os.WriteFile(filepath.Join(custom, "plugin.yaml"), []byte("name: my-own-plugin\n"), 0o600); err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + if err := CopyPlugins(dst); err != nil { |
| 129 | + t.Fatalf("CopyPlugins: %v", err) |
| 130 | + } |
| 131 | + if _, err := os.Stat(filepath.Join(custom, "plugin.yaml")); err != nil { |
| 132 | + t.Errorf("user plugin was clobbered by re-seed: %v", err) |
| 133 | + } |
| 134 | + if _, err := os.Stat(filepath.Join(dst, "pay_mcp", "__init__.py")); err != nil { |
| 135 | + t.Errorf("pay_mcp not seeded alongside user plugin: %v", err) |
| 136 | + } |
| 137 | +} |
0 commit comments