|
| 1 | +// Copyright 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net" |
| 20 | + "os" |
| 21 | + "os/exec" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +// TestSessionE2E drives the real `lk agent session` lifecycle end to end: |
| 32 | +// build the binary, `start` the detached daemon, `say` to make the model echo |
| 33 | +// a token (asserting the CLI→daemon→agent→LLM round-trip), `end`, then confirm |
| 34 | +// the daemon exited (nothing answers on the port). |
| 35 | +// |
| 36 | +// Opt-in: needs a prepared agent venv + live creds, so it skips unless |
| 37 | +// LIVEKIT_API_KEY is set. Defaults to testdata/echo-agent; override with LK_SESSION_E2E_AGENT. |
| 38 | +func TestSessionE2E(t *testing.T) { |
| 39 | + if os.Getenv("LIVEKIT_API_KEY") == "" { |
| 40 | + t.Skip("set LIVEKIT_API_KEY (and prepare the agent venv) to run the session e2e test") |
| 41 | + } |
| 42 | + entrypoint := os.Getenv("LK_SESSION_E2E_AGENT") |
| 43 | + if entrypoint == "" { |
| 44 | + entrypoint = filepath.Join("testdata", "echo-agent", "agent.py") |
| 45 | + } |
| 46 | + entrypoint, err := filepath.Abs(entrypoint) |
| 47 | + require.NoError(t, err) |
| 48 | + require.FileExists(t, entrypoint, "agent entrypoint not found (set LK_SESSION_E2E_AGENT to override)") |
| 49 | + |
| 50 | + // Dedicated port so the test can't collide with a real session on 8775. |
| 51 | + port := "18775" |
| 52 | + if p := os.Getenv("LK_SESSION_E2E_PORT"); p != "" { |
| 53 | + port = p |
| 54 | + } |
| 55 | + |
| 56 | + bin := buildLK(t) |
| 57 | + |
| 58 | + run := func(timeout time.Duration, args ...string) (string, error) { |
| 59 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 60 | + defer cancel() |
| 61 | + cmd := exec.CommandContext(ctx, bin, args...) |
| 62 | + cmd.Env = os.Environ() |
| 63 | + out, err := cmd.CombinedOutput() |
| 64 | + return string(out), err |
| 65 | + } |
| 66 | + |
| 67 | + // Best-effort teardown so a mid-run failure doesn't leave the daemon alive. |
| 68 | + t.Cleanup(func() { |
| 69 | + _, _ = run(15*time.Second, "agent", "session", "end", "--port", port) |
| 70 | + }) |
| 71 | + |
| 72 | + // start: launches the detached daemon and returns once the agent is ready. |
| 73 | + startOut, err := run(90*time.Second, "agent", "session", "start", "--port", port, entrypoint) |
| 74 | + require.NoError(t, err, "session start failed:\n%s", startOut) |
| 75 | + require.Contains(t, startOut, "Session started.", "start did not report readiness:\n%s", startOut) |
| 76 | + |
| 77 | + // say: the token appears once in the echoed prompt and again in the reply, so |
| 78 | + // >=2 occurrences proves the agent answered, not just the local echo. |
| 79 | + token := "PINEAPPLE7351" |
| 80 | + sayOut, err := run(90*time.Second, "agent", "session", "say", "--port", port, |
| 81 | + "Repeat this token back to me exactly and nothing else: "+token) |
| 82 | + require.NoError(t, err, "session say failed:\n%s", sayOut) |
| 83 | + require.GreaterOrEqualf(t, strings.Count(sayOut, token), 2, |
| 84 | + "agent did not echo the token back; say output:\n%s", sayOut) |
| 85 | + |
| 86 | + endOut, err := run(30*time.Second, "agent", "session", "end", "--port", port) |
| 87 | + require.NoError(t, err, "session end failed:\n%s", endOut) |
| 88 | + require.Contains(t, endOut, "Session ended.", "end did not confirm shutdown:\n%s", endOut) |
| 89 | + |
| 90 | + // The detached daemon should now be gone: nothing should answer on the port. |
| 91 | + require.Eventually(t, func() bool { |
| 92 | + conn, derr := net.DialTimeout("tcp", "127.0.0.1:"+port, 200*time.Millisecond) |
| 93 | + if derr != nil { |
| 94 | + return true // refused → daemon exited |
| 95 | + } |
| 96 | + conn.Close() |
| 97 | + return false |
| 98 | + }, 10*time.Second, 200*time.Millisecond, "session daemon still listening on port %s after end", port) |
| 99 | +} |
| 100 | + |
| 101 | +// buildLK compiles the lk binary into a temp dir and returns its path. |
| 102 | +func buildLK(t *testing.T) string { |
| 103 | + t.Helper() |
| 104 | + bin := filepath.Join(t.TempDir(), "lk") |
| 105 | + if runtime.GOOS == "windows" { |
| 106 | + bin += ".exe" |
| 107 | + } |
| 108 | + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) |
| 109 | + defer cancel() |
| 110 | + build := exec.CommandContext(ctx, "go", "build", "-o", bin, ".") |
| 111 | + out, err := build.CombinedOutput() |
| 112 | + require.NoErrorf(t, err, "failed to build lk binary:\n%s", out) |
| 113 | + return bin |
| 114 | +} |
0 commit comments