waveterm/pkg/gogen/gogen_test.go
Copilot df24959e23
Add native 2+ arg RPC support and wire a concrete TestMultiArgCommand through server, generated clients, and CLI (#2963)
This PR extends WSH RPC command signatures to support `ctx + 2+ typed
args` while preserving existing `ctx` and `ctx + 1 arg` behavior. It
also adds a concrete `TestMultiArgCommand` end-to-end so the generated
Go/TS client surfaces can be inspected and exercised from CLI.

- **RPC wire + dispatch model**
- Added `wshrpc.MultiArg` (`args []any`) as the over-the-wire envelope
for 2+ arg commands.
- Extended RPC metadata to track all command arg types
(`CommandDataTypes`) and exposed a helper for normalized access.
  - Updated server adapter unmarshalling to:
    - decode `MultiArg` for 2+ arg commands,
    - validate arg count,
- re-unmarshal each arg into its declared type before invoking typed
handlers.
  - Kept single-arg commands on the existing non-`MultiArg` path.

- **Code generation (Go + TS)**
- Go codegen now emits multi-parameter wrappers for 2+ arg methods and
packs payload as `wshrpc.MultiArg`.
- TS codegen now emits multi-parameter API methods and packs payload as
`{ args: [...] }`.
  - 0/1-arg generation remains unchanged to avoid wire/API churn.

- **Concrete command added for validation**
  - Added to `WshRpcInterface`:
- `TestMultiArgCommand(ctx context.Context, arg1 string, arg2 int, arg3
bool) (string, error)`
- Implemented in `wshserver` with deterministic formatted return output
including source + all args.
- Updated `wsh test` command to call `TestMultiArgCommand` and print the
returned string.

- **Focused coverage**
- Added/updated targeted tests around RPC metadata and Go/TS multi-arg
codegen behavior, including command declaration for `testmultiarg`.

Example generated call shape:

```go
func TestMultiArgCommand(w *wshutil.WshRpc, arg1 string, arg2 int, arg3 bool, opts *wshrpc.RpcOpts) (string, error) {
    return sendRpcRequestCallHelper[string](
        w,
        "testmultiarg",
        wshrpc.MultiArg{Args: []any{arg1, arg2, arg3}},
        opts,
    )
}
```

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sawka <2722291+sawka@users.noreply.github.com>
Co-authored-by: sawka <mike@commandline.dev>
2026-03-02 12:29:04 -08:00

46 lines
1.3 KiB
Go

// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package gogen
import (
"reflect"
"strings"
"testing"
"github.com/wavetermdev/waveterm/pkg/wshrpc"
)
func TestGetWshMethodDataParamsAndExpr_MultiArg(t *testing.T) {
methodDecl := &wshrpc.WshRpcMethodDecl{
CommandDataTypes: []reflect.Type{
reflect.TypeOf(""),
reflect.TypeOf(0),
},
}
params, expr := getWshMethodDataParamsAndExpr(methodDecl)
if params != ", arg1 string, arg2 int" {
t.Fatalf("unexpected params: %q", params)
}
if expr != "wshrpc.MultiArg{Args: []any{arg1, arg2}}" {
t.Fatalf("unexpected expr: %q", expr)
}
}
func TestGenMethodCall_MultiArg(t *testing.T) {
methodDecl := &wshrpc.WshRpcMethodDecl{
Command: "test",
CommandType: wshrpc.RpcType_Call,
MethodName: "TestCommand",
CommandDataTypes: []reflect.Type{reflect.TypeOf(""), reflect.TypeOf(0)},
}
var sb strings.Builder
GenMethod_Call(&sb, methodDecl)
out := sb.String()
if !strings.Contains(out, "func TestCommand(w *wshutil.WshRpc, arg1 string, arg2 int, opts *wshrpc.RpcOpts) error {") {
t.Fatalf("generated method missing multi-arg signature:\n%s", out)
}
if !strings.Contains(out, "sendRpcRequestCallHelper[any](w, \"test\", wshrpc.MultiArg{Args: []any{arg1, arg2}}, opts)") {
t.Fatalf("generated method missing MultiArg payload:\n%s", out)
}
}