Files
nexus/internal/recording/recorder_test.go
T
andy 8c9ce67684 Phase 3: Session recording/playback and encrypted profile export/import
Session Recording & Playback:
- Add recording package with asciicast v2 format writer and reader
- Recorder captures terminal I/O with timestamps from SSH and Telnet sessions
- Input and output events recorded via hooks in session manager callbacks
- Recording store manages .cast files in ~/Library/Application Support/NexusTerminal/recordings
- Recording browser modal lists all saved recordings with metadata
- Full playback viewer with embedded xterm.js terminal, progress scrubber,
  play/pause/restart controls, and 0.5x-10x speed adjustment
- Recording indicator (pulsing red dot) shown on active tab during capture
- Keyboard shortcut Cmd+Shift+R to toggle recording on active session
- Tests for recording, store listing, deletion, and double-close safety

Encrypted Profile Export/Import:
- Add exportimport package with AES-256-GCM encryption (Argon2id key derivation)
- File format: version byte + 32-byte salt + 12-byte nonce + ciphertext
- Same Argon2id parameters as vault (128 MiB, 3 iterations, 4 threads)
- Export modal with profile selection checkboxes and password entry
- Import modal with password entry and conflict strategy (skip/overwrite/duplicate)
- Native save/open file dialogs for .nexus files
- Tests for round-trip encryption, wrong password rejection, corrupt files
- Replaced placeholder import/export with full encrypted implementation

UI:
- Added export and recordings buttons to sidebar footer
- Recording and playback CSS with charcoal design tokens
- Wired RecordingManager and ExportImportUI into app.js
2026-04-16 00:00:00 +01:00

166 lines
3.9 KiB
Go

package recording
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestRecorderWriteAndClose(t *testing.T) {
dir := t.TempDir()
id := "test-session-001"
rec, err := NewRecorder(dir, id, "Test Recording", 80, 24)
if err != nil {
t.Fatalf("NewRecorder failed: %v", err)
}
// Write some output events.
rec.WriteOutput([]byte("Welcome to the server\r\n"))
rec.WriteOutput([]byte("$ "))
// Write some input events.
rec.WriteInput([]byte("ls -la\r\n"))
// Write more output.
rec.WriteOutput([]byte("total 42\r\n"))
if rec.EventCount() != 4 {
t.Errorf("expected 4 events, got %d", rec.EventCount())
}
if err := rec.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
// Verify the file exists and has correct format.
path := filepath.Join(dir, id+".cast")
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("failed to read recording: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
if len(lines) < 5 { // 1 header + 4 events
t.Fatalf("expected at least 5 lines, got %d", len(lines))
}
// Verify header.
var hdr Header
if err := json.Unmarshal([]byte(lines[0]), &hdr); err != nil {
t.Fatalf("failed to parse header: %v", err)
}
if hdr.Version != 2 {
t.Errorf("expected version 2, got %d", hdr.Version)
}
if hdr.Width != 80 || hdr.Height != 24 {
t.Errorf("expected 80x24, got %dx%d", hdr.Width, hdr.Height)
}
// Verify first event is output.
var evt []json.RawMessage
if err := json.Unmarshal([]byte(lines[1]), &evt); err != nil {
t.Fatalf("failed to parse first event: %v", err)
}
if len(evt) != 3 {
t.Fatalf("expected 3-element array, got %d", len(evt))
}
var evtType string
json.Unmarshal(evt[1], &evtType)
if evtType != "o" {
t.Errorf("expected event type 'o', got %q", evtType)
}
}
func TestStoreListAndGet(t *testing.T) {
dir := t.TempDir()
// Create a recording.
rec, err := NewRecorder(dir, "session-abc", "My Session", 120, 40)
if err != nil {
t.Fatalf("NewRecorder failed: %v", err)
}
rec.WriteOutput([]byte("hello world\r\n"))
time.Sleep(10 * time.Millisecond)
rec.WriteOutput([]byte("done\r\n"))
rec.Close()
// Use the store to list.
store := NewStore(dir)
metas, err := store.List()
if err != nil {
t.Fatalf("List failed: %v", err)
}
if len(metas) != 1 {
t.Fatalf("expected 1 recording, got %d", len(metas))
}
if metas[0].ID != "session-abc" {
t.Errorf("expected ID 'session-abc', got %q", metas[0].ID)
}
if metas[0].Width != 120 || metas[0].Height != 40 {
t.Errorf("unexpected dimensions: %dx%d", metas[0].Width, metas[0].Height)
}
// Read full recording.
hdr, events, err := store.Get("session-abc")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if hdr.Title != "My Session" {
t.Errorf("expected title 'My Session', got %q", hdr.Title)
}
if len(events) != 2 {
t.Errorf("expected 2 events, got %d", len(events))
}
}
func TestStoreDelete(t *testing.T) {
dir := t.TempDir()
rec, err := NewRecorder(dir, "to-delete", "Delete Me", 80, 24)
if err != nil {
t.Fatalf("NewRecorder failed: %v", err)
}
rec.WriteOutput([]byte("test\n"))
rec.Close()
store := NewStore(dir)
// Verify it exists.
metas, _ := store.List()
if len(metas) != 1 {
t.Fatalf("expected 1 recording, got %d", len(metas))
}
// Delete.
if err := store.Delete("to-delete"); err != nil {
t.Fatalf("Delete failed: %v", err)
}
// Verify it's gone.
metas, _ = store.List()
if len(metas) != 0 {
t.Errorf("expected 0 recordings, got %d", len(metas))
}
}
func TestDoubleClose(t *testing.T) {
dir := t.TempDir()
rec, err := NewRecorder(dir, "double-close", "Test", 80, 24)
if err != nil {
t.Fatalf("NewRecorder failed: %v", err)
}
rec.WriteOutput([]byte("test"))
if err := rec.Close(); err != nil {
t.Fatalf("first Close failed: %v", err)
}
// Second close should be a no-op.
if err := rec.Close(); err != nil {
t.Fatalf("second Close failed: %v", err)
}
}