-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscripts_runtime_test.go
More file actions
76 lines (70 loc) · 2.52 KB
/
Copy pathscripts_runtime_test.go
File metadata and controls
76 lines (70 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestUserScriptInventoryMergesValidatedRuntimeStatus(t *testing.T) {
configHome := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", configHome)
path := filepath.Join(userScriptsDir(), "runtime.js")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("window.runtimeTest = true;"), 0o644); err != nil {
t.Fatal(err)
}
inventory := scanUserScripts(map[string]any{
"user:runtime.js": map[string]any{"status": "failed", "error": "boom"},
"user:missing.js": map[string]any{"status": "loaded"},
})
if len(inventory.Scripts) != 1 {
t.Fatalf("scripts = %#v", inventory.Scripts)
}
item := inventory.Scripts[0]
if item.Status != "failed" || item.Error != "boom" {
t.Fatalf("runtime status was not merged: %#v", item)
}
inventory = scanUserScripts(map[string]any{
"scripts": map[string]any{"user:runtime.js": map[string]any{"status": "arbitrary", "error": strings.Repeat("x", 5000)}},
})
item = inventory.Scripts[0]
if item.Status != "not_loaded" || len([]rune(item.Error)) != 4000 {
t.Fatalf("runtime status should be normalized and bounded: %#v", item)
}
}
func TestEnabledUserScriptBundleRecordsRuntimeLifecycle(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
path := filepath.Join(userScriptsDir(), "lifecycle.js")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("window.lifecycleTest = true;"), 0o644); err != nil {
t.Fatal(err)
}
bundle := enabledUserScriptBundle()
for _, expected := range []string{"__codexPlusUserScripts", `status = "loaded"`, `status = "failed"`, `user:lifecycle.js`} {
if !strings.Contains(bundle, expected) {
t.Fatalf("bundle missing %q:\n%s", expected, bundle)
}
}
}
func TestUserScriptListBridgeAcceptsRendererRuntimeStatus(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
path := filepath.Join(userScriptsDir(), "bridge.js")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("window.bridgeTest = true;"), 0o644); err != nil {
t.Fatal(err)
}
result := (&launcherRuntime{}).handleBridgeRequest("/user-scripts/list", json.RawMessage(`{
"runtime_status": {"user:bridge.js": {"status": "loaded", "error": ""}}
}`))
items, ok := result["scripts"].([]userScriptInventoryItem)
if !ok || len(items) != 1 || items[0].Status != "loaded" {
t.Fatalf("bridge runtime status was not merged: %#v", result)
}
}