|
| 1 | +package portshim_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/aoagents/agent-orchestrator/backend/internal/adapters/agent" |
| 10 | + "github.com/aoagents/agent-orchestrator/backend/internal/adapters/agent/portshim" |
| 11 | + "github.com/aoagents/agent-orchestrator/backend/internal/ports" |
| 12 | + "github.com/aoagents/agent-orchestrator/backend/internal/session" |
| 13 | +) |
| 14 | + |
| 15 | +type fakeAgent struct { |
| 16 | + launchCmd []string |
| 17 | + launchErr error |
| 18 | + restoreCmd []string |
| 19 | + restoreOK bool |
| 20 | + restoreErr error |
| 21 | + gotLaunchCfg agent.LaunchConfig |
| 22 | + gotRestoreCfg agent.RestoreConfig |
| 23 | +} |
| 24 | + |
| 25 | +func (f *fakeAgent) GetConfigSpec(context.Context) (agent.ConfigSpec, error) { |
| 26 | + return agent.ConfigSpec{}, nil |
| 27 | +} |
| 28 | +func (f *fakeAgent) GetLaunchCommand(_ context.Context, cfg agent.LaunchConfig) ([]string, error) { |
| 29 | + f.gotLaunchCfg = cfg |
| 30 | + return f.launchCmd, f.launchErr |
| 31 | +} |
| 32 | +func (f *fakeAgent) GetPromptDeliveryStrategy(context.Context, agent.LaunchConfig) (agent.PromptDeliveryStrategy, error) { |
| 33 | + return agent.PromptDeliveryInCommand, nil |
| 34 | +} |
| 35 | +func (f *fakeAgent) GetAgentHooks(context.Context, agent.WorkspaceHookConfig) error { return nil } |
| 36 | +func (f *fakeAgent) GetRestoreCommand(_ context.Context, cfg agent.RestoreConfig) ([]string, bool, error) { |
| 37 | + f.gotRestoreCfg = cfg |
| 38 | + return f.restoreCmd, f.restoreOK, f.restoreErr |
| 39 | +} |
| 40 | +func (f *fakeAgent) SessionInfo(context.Context, agent.SessionRef) (agent.SessionInfo, bool, error) { |
| 41 | + return agent.SessionInfo{}, false, nil |
| 42 | +} |
| 43 | + |
| 44 | +func TestSatisfiesPortsAgent(t *testing.T) { |
| 45 | + var _ ports.Agent = (*portshim.Shim)(nil) |
| 46 | +} |
| 47 | + |
| 48 | +func TestGetLaunchCommand_JoinsArgvShellSafely(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + name string |
| 51 | + argv []string |
| 52 | + want string |
| 53 | + }{ |
| 54 | + {"simple", []string{"claude"}, "claude"}, |
| 55 | + {"flags and prompt", []string{"claude", "--", "do it"}, "claude -- 'do it'"}, |
| 56 | + {"path with spaces", []string{"/Applications/My App/claude", "--flag"}, "'/Applications/My App/claude' --flag"}, |
| 57 | + {"prompt with single quote", []string{"claude", "--", "it's fine"}, `claude -- 'it'\''s fine'`}, |
| 58 | + {"empty argv", []string{}, ""}, |
| 59 | + } |
| 60 | + for _, tc := range tests { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + s := portshim.New(&fakeAgent{launchCmd: tc.argv}) |
| 63 | + got := s.GetLaunchCommand(ports.AgentConfig{}) |
| 64 | + if got != tc.want { |
| 65 | + t.Fatalf("got %q want %q", got, tc.want) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestGetLaunchCommand_PropagatesAgentConfig(t *testing.T) { |
| 72 | + fake := &fakeAgent{launchCmd: []string{"claude"}} |
| 73 | + s := portshim.New(fake) |
| 74 | + cfg := ports.AgentConfig{SessionID: "p-1", WorkspacePath: "/ws/p-1", Prompt: "hello"} |
| 75 | + _ = s.GetLaunchCommand(cfg) |
| 76 | + if fake.gotLaunchCfg.SessionID != "p-1" { |
| 77 | + t.Errorf("SessionID not propagated: %+v", fake.gotLaunchCfg) |
| 78 | + } |
| 79 | + if fake.gotLaunchCfg.WorkspacePath != "/ws/p-1" { |
| 80 | + t.Errorf("WorkspacePath not propagated: %+v", fake.gotLaunchCfg) |
| 81 | + } |
| 82 | + if fake.gotLaunchCfg.Prompt != "hello" { |
| 83 | + t.Errorf("Prompt not propagated: %+v", fake.gotLaunchCfg) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestGetLaunchCommand_AgentErrorReturnsEmpty(t *testing.T) { |
| 88 | + fake := &fakeAgent{launchErr: errors.New("boom")} |
| 89 | + s := portshim.New(fake) |
| 90 | + got := s.GetLaunchCommand(ports.AgentConfig{SessionID: "p-1"}) |
| 91 | + if got != "" { |
| 92 | + t.Fatalf("expected empty on error, got %q", got) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestGetEnvironment_ReturnsAgentEnvKeysOnly(t *testing.T) { |
| 97 | + // The richer Agent interface doesn't carry the env keys the SM port supplies, |
| 98 | + // so the shim has nothing agent-specific to surface. SM layers AO_* on top. |
| 99 | + s := portshim.New(&fakeAgent{}) |
| 100 | + got := s.GetEnvironment(ports.AgentConfig{SessionID: "p-1"}) |
| 101 | + if len(got) != 0 { |
| 102 | + t.Fatalf("expected empty env from shim, got %v", got) |
| 103 | + } |
| 104 | + for _, k := range []string{session.EnvSessionID, session.EnvProjectID, session.EnvIssueID} { |
| 105 | + if _, ok := got[k]; ok { |
| 106 | + t.Errorf("shim must not pre-populate AO env key %s; SM owns it", k) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetRestoreCommand_JoinsWhenOK(t *testing.T) { |
| 112 | + fake := &fakeAgent{restoreCmd: []string{"claude", "--resume", "abc 123"}, restoreOK: true} |
| 113 | + s := portshim.New(fake) |
| 114 | + got := s.GetRestoreCommand("abc 123") |
| 115 | + want := `claude --resume 'abc 123'` |
| 116 | + if got != want { |
| 117 | + t.Fatalf("got %q want %q", got, want) |
| 118 | + } |
| 119 | + if fake.gotRestoreCfg.Session.ID != "abc 123" { |
| 120 | + t.Errorf("session id not propagated: %+v", fake.gotRestoreCfg) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestGetRestoreCommand_NotOKReturnsEmpty(t *testing.T) { |
| 125 | + fake := &fakeAgent{restoreOK: false} |
| 126 | + s := portshim.New(fake) |
| 127 | + if got := s.GetRestoreCommand("anything"); got != "" { |
| 128 | + t.Fatalf("expected empty when not restorable, got %q", got) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestGetRestoreCommand_ErrorReturnsEmpty(t *testing.T) { |
| 133 | + fake := &fakeAgent{restoreErr: errors.New("boom")} |
| 134 | + s := portshim.New(fake) |
| 135 | + if got := s.GetRestoreCommand("x"); got != "" { |
| 136 | + t.Fatalf("expected empty on restore error, got %q", got) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestGetRestoreCommand_PassesAgentSessionIDAsMetadata(t *testing.T) { |
| 141 | + // Claude-code (and Codex) read the native session id off cfg.Session.Metadata |
| 142 | + // ["agentSessionId"] to rebuild the --resume command. Pass it via both Session.ID |
| 143 | + // (the legacy fallback) and Session.Metadata so the richer adapter can find it. |
| 144 | + fake := &fakeAgent{restoreCmd: []string{"claude", "--resume", "x"}, restoreOK: true} |
| 145 | + s := portshim.New(fake) |
| 146 | + _ = s.GetRestoreCommand("native-uuid") |
| 147 | + gotID := fake.gotRestoreCfg.Session.ID |
| 148 | + if gotID != "native-uuid" { |
| 149 | + t.Errorf("Session.ID want native-uuid, got %q", gotID) |
| 150 | + } |
| 151 | + if m := fake.gotRestoreCfg.Session.Metadata[agent.MetadataKeyAgentSessionID]; m != "native-uuid" { |
| 152 | + t.Errorf("Session.Metadata[%s] want native-uuid, got %q", agent.MetadataKeyAgentSessionID, m) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestShellQuotingDoesNotDoubleQuoteSafeStrings(t *testing.T) { |
| 157 | + // Safe identifiers (letters, digits, dash, dot, slash, underscore) should |
| 158 | + // pass through unquoted; quoting them would inflate every command. |
| 159 | + s := portshim.New(&fakeAgent{launchCmd: []string{"/usr/local/bin/claude", "--session-id", "abc-123_xyz.uuid"}}) |
| 160 | + got := s.GetLaunchCommand(ports.AgentConfig{}) |
| 161 | + if strings.Contains(got, "'") { |
| 162 | + t.Fatalf("got unexpected quotes: %q", got) |
| 163 | + } |
| 164 | +} |
0 commit comments