返回 DeepSeek-Reasonix
attach_dropped_test.go
根目录 / desktop / attach_dropped_test.go
1 package main
2
3 import (
4 "context"
5 "encoding/base64"
6 "os"
7 "path/filepath"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "reasonix/internal/config"
14 "reasonix/internal/control"
15 "reasonix/internal/event"
16 )
17
18 const desktopTinyPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
19
20 func TestWorkspaceRelativeIn(t *testing.T) {
21 root := t.TempDir()
22
23 if rel, ok := workspaceRelativeIn(filepath.Join(root, "sub", "file.go"), root); !ok || rel != "sub/file.go" {
24 t.Fatalf("in-tree = (%q, %v), want (sub/file.go, true)", rel, ok)
25 }
26 if _, ok := workspaceRelativeIn(filepath.Join(filepath.Dir(root), "sibling.txt"), root); ok {
27 t.Fatal("a path above the workspace must not resolve as in-tree")
28 }
29 }
30
31 func TestIsImageExt(t *testing.T) {
32 for _, p := range []string{"a.png", "A.PNG", "b.jpeg", "c.webp"} {
33 if !isImageExt(p) {
34 t.Errorf("%q should be an image extension", p)
35 }
36 }
37 for _, p := range []string{"notes.pdf", "main.go", "noext"} {
38 if isImageExt(p) {
39 t.Errorf("%q should not be an image extension", p)
40 }
41 }
42 }
43
44 func TestSavePastedImageUsesActiveWorkspaceRoot(t *testing.T) {
45 orig, _ := os.Getwd()
46 defer os.Chdir(orig)
47
48 launchRoot := t.TempDir()
49 projectRoot := t.TempDir()
50 if err := os.Chdir(projectRoot); err != nil {
51 t.Fatal(err)
52 }
53 projectRoot, _ = os.Getwd()
54 if err := os.Chdir(launchRoot); err != nil {
55 t.Fatal(err)
56 }
57 app := &App{
58 tabs: map[string]*WorkspaceTab{
59 "project": {ID: "project", WorkspaceRoot: projectRoot},
60 },
61 activeTabID: "project",
62 }
63
64 got, err := app.SavePastedImage("data:image/png;base64," + desktopTinyPNG)
65 if err != nil {
66 t.Fatalf("SavePastedImage: %v", err)
67 }
68 if _, err := os.Stat(filepath.Join(projectRoot, filepath.FromSlash(got))); err != nil {
69 t.Fatalf("pasted image should be saved under active workspace: %v", err)
70 }
71 if _, err := os.Stat(filepath.Join(launchRoot, filepath.FromSlash(got))); !os.IsNotExist(err) {
72 t.Fatalf("pasted image should not be saved under launch root, stat err=%v", err)
73 }
74 preview, err := app.AttachmentDataURL(got)
75 if err != nil {
76 t.Fatalf("AttachmentDataURL: %v", err)
77 }
78 if !strings.HasPrefix(preview, "data:image/png;base64,") {
79 t.Fatalf("preview = %q, want png data URL", preview)
80 }
81 }
82
83 func TestReadSessionAttachmentForTabRequiresCanonicalSession(t *testing.T) {
84 app := &App{
85 tabs: map[string]*WorkspaceTab{
86 "a": {ID: "a", WorkspaceRoot: t.TempDir(), SessionGeneration: 1},
87 },
88 activeTabID: "a",
89 }
90 if _, err := app.ReadSessionAttachmentForTab("a", strings.Repeat("ab", 32), 0); err == nil {
91 t.Fatal("ReadSessionAttachmentForTab accepted a tab without a canonical session")
92 }
93 }
94
95 func TestStageImageForTabRequiresBoundController(t *testing.T) {
96 app := &App{
97 tabs: map[string]*WorkspaceTab{
98 "a": {ID: "a", WorkspaceRoot: t.TempDir()},
99 },
100 activeTabID: "a",
101 }
102 if _, err := app.StageImageForTab("a", "op-1", "shot.png", "image/png", "data:image/png;base64,"+desktopTinyPNG); err == nil {
103 t.Fatal("StageImageForTab accepted a tab without a session controller")
104 }
105 }
106
107 func TestLegacyAttachmentRPCRejectsMissingTarget(t *testing.T) {
108 t.Chdir(t.TempDir())
109 app := &App{tabs: map[string]*WorkspaceTab{}}
110 if _, err := app.SavePastedImage("data:image/png;base64," + desktopTinyPNG); err == nil {
111 t.Fatal("legacy attachment RPC fell back to the process working directory")
112 }
113 if _, err := os.Stat(filepath.Join(".reasonix", "attachments")); !os.IsNotExist(err) {
114 t.Fatalf("missing target created a process-relative attachment directory: %v", err)
115 }
116 }
117
118 func TestGlobalAttachmentRPCUsesStableGlobalWorkspace(t *testing.T) {
119 isolateDesktopUserDirs(t)
120 launchRoot := t.TempDir()
121 t.Chdir(launchRoot)
122 app := &App{
123 tabs: map[string]*WorkspaceTab{"global": {ID: "global", Scope: "global"}},
124 activeTabID: "global",
125 }
126 rel, err := app.SavePastedImage("data:image/png;base64," + desktopTinyPNG)
127 if err != nil {
128 t.Fatal(err)
129 }
130 if _, err := os.Stat(filepath.Join(globalWorkspaceRoot(), filepath.FromSlash(rel))); err != nil {
131 t.Fatalf("global attachment missing from stable global workspace: %v", err)
132 }
133 if _, err := os.Stat(filepath.Join(launchRoot, filepath.FromSlash(rel))); !os.IsNotExist(err) {
134 t.Fatalf("global attachment was written under process cwd: %v", err)
135 }
136 }
137
138 func TestSavePastedImageForTabKeepsCapturedWorkspaceAcrossActiveSwitch(t *testing.T) {
139 launchRoot := t.TempDir()
140 projectA := t.TempDir()
141 projectB := t.TempDir()
142 t.Chdir(launchRoot)
143 app := &App{
144 tabs: map[string]*WorkspaceTab{
145 "a": {ID: "a", WorkspaceRoot: projectA},
146 "b": {ID: "b", WorkspaceRoot: projectB},
147 },
148 activeTabID: "a",
149 }
150 app.attachmentIOHook = func() {
151 app.mu.Lock()
152 app.activeTabID = "b"
153 app.mu.Unlock()
154 }
155
156 before, err := os.Getwd()
157 if err != nil {
158 t.Fatal(err)
159 }
160 got, err := app.SavePastedImageForTab("a", "data:image/png;base64,"+desktopTinyPNG)
161 if err != nil {
162 t.Fatalf("SavePastedImageForTab: %v", err)
163 }
164 after, err := os.Getwd()
165 if err != nil {
166 t.Fatal(err)
167 }
168 if before != after {
169 t.Fatalf("process cwd changed from %q to %q", before, after)
170 }
171 if _, err := os.Stat(filepath.Join(projectA, filepath.FromSlash(got))); err != nil {
172 t.Fatalf("captured workspace attachment missing: %v", err)
173 }
174 if _, err := os.Stat(filepath.Join(projectB, filepath.FromSlash(got))); !os.IsNotExist(err) {
175 t.Fatalf("attachment drifted to active workspace B: %v", err)
176 }
177 }
178
179 func TestAttachmentDataURLForTabIsolatesSameRelativePath(t *testing.T) {
180 projectA := t.TempDir()
181 projectB := t.TempDir()
182 rel := ".reasonix/attachments/shared.png"
183 rawA, err := base64.StdEncoding.DecodeString(desktopTinyPNG)
184 if err != nil {
185 t.Fatal(err)
186 }
187 rawB := append([]byte(nil), rawA...)
188 rawB[len(rawB)-1] ^= 1
189 for root, raw := range map[string][]byte{projectA: rawA, projectB: rawB} {
190 path := filepath.Join(root, filepath.FromSlash(rel))
191 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
192 t.Fatal(err)
193 }
194 if err := os.WriteFile(path, raw, 0o644); err != nil {
195 t.Fatal(err)
196 }
197 }
198 app := &App{tabs: map[string]*WorkspaceTab{
199 "a": {ID: "a", WorkspaceRoot: projectA},
200 "b": {ID: "b", WorkspaceRoot: projectB},
201 }}
202
203 one, err := app.AttachmentDataURLForTab("a", rel)
204 if err != nil {
205 t.Fatal(err)
206 }
207 two, err := app.AttachmentDataURLForTab("b", rel)
208 if err != nil {
209 t.Fatal(err)
210 }
211 if one == two {
212 t.Fatal("same relative attachment path resolved to the same workspace bytes")
213 }
214 }
215
216 func TestSavePastedImageForTabRejectsClosedTargetAndRemovesWrite(t *testing.T) {
217 projectRoot := t.TempDir()
218 tab := &WorkspaceTab{ID: "a", WorkspaceRoot: projectRoot}
219 app := &App{tabs: map[string]*WorkspaceTab{"a": tab}, activeTabID: "a"}
220 var once sync.Once
221 app.attachmentIOHook = func() {
222 once.Do(func() {
223 app.mu.Lock()
224 delete(app.tabs, "a")
225 app.mu.Unlock()
226 })
227 }
228
229 if _, err := app.SavePastedImageForTab("a", "data:image/png;base64,"+desktopTinyPNG); err == nil {
230 t.Fatal("closed attachment target was accepted")
231 }
232 entries, err := os.ReadDir(filepath.Join(projectRoot, ".reasonix", "attachments"))
233 if err != nil {
234 t.Fatal(err)
235 }
236 if len(entries) != 0 {
237 t.Fatalf("rejected attachment write left files: %v", entries)
238 }
239 }
240
241 func TestAttachmentDataURLForTabRejectsReplacedRuntime(t *testing.T) {
242 projectRoot := t.TempDir()
243 rel, err := control.SaveImageBytesInRoot(projectRoot, "image/png", mustDecodeBase64(t, desktopTinyPNG))
244 if err != nil {
245 t.Fatal(err)
246 }
247 first := control.New(control.Options{WorkspaceRoot: projectRoot})
248 second := control.New(control.Options{WorkspaceRoot: projectRoot})
249 t.Cleanup(first.Close)
250 t.Cleanup(second.Close)
251 tab := &WorkspaceTab{ID: "a", WorkspaceRoot: projectRoot, Ctrl: first}
252 app := &App{tabs: map[string]*WorkspaceTab{"a": tab}, activeTabID: "a"}
253 var once sync.Once
254 app.attachmentIOHook = func() {
255 once.Do(func() {
256 app.mu.Lock()
257 tab.Ctrl = second
258 app.mu.Unlock()
259 })
260 }
261
262 if _, err := app.AttachmentDataURLForTab("a", rel); err == nil {
263 t.Fatal("preview from replaced runtime was accepted")
264 }
265 }
266
267 func mustDecodeBase64(t *testing.T, value string) []byte {
268 t.Helper()
269 raw, err := base64.StdEncoding.DecodeString(value)
270 if err != nil {
271 t.Fatal(err)
272 }
273 return raw
274 }
275
276 func TestSavePastedImageUsesPinnedSessionOwnerBeforeStaleWorkspaceRoot(t *testing.T) {
277 isolateDesktopUserDirs(t)
278 orig, _ := os.Getwd()
279 defer os.Chdir(orig)
280
281 launchRoot := t.TempDir()
282 projectA := t.TempDir()
283 projectB := t.TempDir()
284 if err := addProject(projectA, "Project A"); err != nil {
285 t.Fatalf("add project A: %v", err)
286 }
287 if err := addProject(projectB, "Project B"); err != nil {
288 t.Fatalf("add project B: %v", err)
289 }
290 sessionDirA := desktopSessionDir(projectA)
291 if err := os.MkdirAll(sessionDirA, 0o755); err != nil {
292 t.Fatalf("mkdir project A sessions: %v", err)
293 }
294 sessionPathA := writeTopicSessionWithPrompt(t, sessionDirA, "project-a.jsonl", "topic_attach_owner", "Attach owner", projectA, "project A prompt", time.Now())
295 if err := os.Chdir(launchRoot); err != nil {
296 t.Fatal(err)
297 }
298
299 app := &App{
300 tabs: map[string]*WorkspaceTab{
301 "project": {ID: "project", Scope: "project", WorkspaceRoot: projectB, SessionPath: sessionPathA},
302 },
303 activeTabID: "project",
304 }
305
306 got, err := app.SavePastedImage("data:image/png;base64," + desktopTinyPNG)
307 if err != nil {
308 t.Fatalf("SavePastedImage: %v", err)
309 }
310 if _, err := os.Stat(filepath.Join(projectA, filepath.FromSlash(got))); err != nil {
311 t.Fatalf("pasted image should be saved under pinned session owner project A: %v", err)
312 }
313 if _, err := os.Stat(filepath.Join(projectB, filepath.FromSlash(got))); !os.IsNotExist(err) {
314 t.Fatalf("pasted image should not be saved under stale project B, stat err=%v", err)
315 }
316 if gotRoot := normalizeProjectRoot(app.tabs["project"].WorkspaceRoot); gotRoot != normalizeProjectRoot(projectA) {
317 t.Fatalf("tab workspace root = %q, want project A %q", gotRoot, normalizeProjectRoot(projectA))
318 }
319 }
320
321 func TestAttachDroppedUsesActiveWorkspaceRoot(t *testing.T) {
322 orig, _ := os.Getwd()
323 defer os.Chdir(orig)
324
325 launchRoot := t.TempDir()
326 projectRoot := t.TempDir()
327 if err := os.Chdir(projectRoot); err != nil {
328 t.Fatal(err)
329 }
330 projectRoot, _ = os.Getwd()
331 if err := os.Chdir(launchRoot); err != nil {
332 t.Fatal(err)
333 }
334 app := &App{
335 tabs: map[string]*WorkspaceTab{
336 "project": {ID: "project", WorkspaceRoot: projectRoot},
337 },
338 activeTabID: "project",
339 }
340 if err := os.MkdirAll(filepath.Join(projectRoot, "sub"), 0o755); err != nil {
341 t.Fatal(err)
342 }
343 target := filepath.Join(projectRoot, "sub", "notes.txt")
344 if err := os.WriteFile(target, []byte("body"), 0o644); err != nil {
345 t.Fatal(err)
346 }
347
348 got, err := app.AttachDropped(target)
349 if err != nil {
350 t.Fatalf("AttachDropped: %v", err)
351 }
352 if got.Kind != "workspace" || got.Path != "sub/notes.txt" {
353 t.Fatalf("got %+v, want workspace ref sub/notes.txt", got)
354 }
355 }
356
357 func TestAttachDroppedImageUsesActiveWorkspaceRoot(t *testing.T) {
358 orig, _ := os.Getwd()
359 defer os.Chdir(orig)
360
361 launchRoot := t.TempDir()
362 projectRoot := t.TempDir()
363 if err := os.Chdir(launchRoot); err != nil {
364 t.Fatal(err)
365 }
366 app := &App{
367 tabs: map[string]*WorkspaceTab{
368 "project": {ID: "project", WorkspaceRoot: projectRoot},
369 },
370 activeTabID: "project",
371 }
372 raw, err := base64.StdEncoding.DecodeString(desktopTinyPNG)
373 if err != nil {
374 t.Fatal(err)
375 }
376 outside := filepath.Join(t.TempDir(), "shot.png")
377 if err := os.WriteFile(outside, raw, 0o644); err != nil {
378 t.Fatal(err)
379 }
380
381 got, err := app.AttachDropped(outside)
382 if err != nil {
383 t.Fatalf("AttachDropped: %v", err)
384 }
385 if got.Kind != "attachment" || !strings.HasSuffix(got.Path, ".png") {
386 t.Fatalf("got %+v, want png attachment", got)
387 }
388 if _, err := os.Stat(filepath.Join(projectRoot, filepath.FromSlash(got.Path))); err != nil {
389 t.Fatalf("dropped image should be saved under active workspace: %v", err)
390 }
391 if _, err := os.Stat(filepath.Join(launchRoot, filepath.FromSlash(got.Path))); !os.IsNotExist(err) {
392 t.Fatalf("dropped image should not be saved under launch root, stat err=%v", err)
393 }
394 if !strings.HasPrefix(got.PreviewURL, "data:image/png;base64,") {
395 t.Fatalf("preview = %q, want png data URL", got.PreviewURL)
396 }
397 }
398
399 func TestAttachDroppedInWorkspaceReferencesInPlace(t *testing.T) {
400 orig, _ := os.Getwd()
401 defer os.Chdir(orig)
402
403 root := t.TempDir()
404 if err := os.Chdir(root); err != nil {
405 t.Fatal(err)
406 }
407 cwd, _ := os.Getwd()
408 if err := os.MkdirAll(filepath.Join(cwd, "sub"), 0o755); err != nil {
409 t.Fatal(err)
410 }
411 target := filepath.Join(cwd, "sub", "notes.txt")
412 if err := os.WriteFile(target, []byte("body"), 0o644); err != nil {
413 t.Fatal(err)
414 }
415 app := &App{
416 tabs: map[string]*WorkspaceTab{"project": {ID: "project", WorkspaceRoot: cwd}},
417 activeTabID: "project",
418 }
419
420 got, err := app.AttachDropped(target)
421 if err != nil {
422 t.Fatalf("AttachDropped: %v", err)
423 }
424 if got.Kind != "workspace" || got.Path != "sub/notes.txt" {
425 t.Fatalf("got %+v, want workspace ref sub/notes.txt", got)
426 }
427 }
428
429 func TestAttachDroppedOutsideWorkspaceReferencesFile(t *testing.T) {
430 orig, _ := os.Getwd()
431 defer os.Chdir(orig)
432
433 outside := filepath.Join(t.TempDir(), "report.pdf")
434 if err := os.WriteFile(outside, []byte("%PDF body"), 0o644); err != nil {
435 t.Fatal(err)
436 }
437
438 root := t.TempDir()
439 if err := os.Chdir(root); err != nil {
440 t.Fatal(err)
441 }
442 c := control.New(control.Options{WorkspaceRoot: root})
443 t.Cleanup(c.Close)
444 app := &App{
445 tabs: map[string]*WorkspaceTab{"project": {ID: "project", WorkspaceRoot: root, Ctrl: c}},
446 activeTabID: "project",
447 }
448
449 got, err := app.AttachDropped(outside)
450 if err != nil {
451 t.Fatalf("AttachDropped: %v", err)
452 }
453 if got.Kind != "workspace" || !filepath.IsAbs(got.Path) || got.DisplayPath == "" {
454 t.Fatalf("got %+v, want exact file reference", got)
455 }
456 }
457
458 func TestAttachDroppedImageStoresThumbnail(t *testing.T) {
459 orig, _ := os.Getwd()
460 defer os.Chdir(orig)
461
462 root := t.TempDir()
463 if err := os.Chdir(root); err != nil {
464 t.Fatal(err)
465 }
466 cwd, _ := os.Getwd()
467 png := append([]byte("\x89PNG\r\n\x1a\n"), make([]byte, 64)...)
468 if err := os.WriteFile(filepath.Join(cwd, "shot.png"), png, 0o644); err != nil {
469 t.Fatal(err)
470 }
471 app := &App{
472 tabs: map[string]*WorkspaceTab{"project": {ID: "project", WorkspaceRoot: cwd}},
473 activeTabID: "project",
474 }
475
476 got, err := app.AttachDropped(filepath.Join(cwd, "shot.png"))
477 if err != nil {
478 t.Fatalf("AttachDropped: %v", err)
479 }
480 if got.Kind != "attachment" || !strings.HasSuffix(got.Path, ".png") {
481 t.Fatalf("got %+v, want png attachment", got)
482 }
483 if !strings.HasPrefix(got.PreviewURL, "data:image/png;base64,") {
484 t.Fatalf("preview = %q, want png data URL", got.PreviewURL)
485 }
486 }
487
488 func TestAttachDroppedOutsideWorkspaceDirRegistersWorkspaceRef(t *testing.T) {
489 orig, _ := os.Getwd()
490 defer os.Chdir(orig)
491
492 workspace := t.TempDir()
493 outside := filepath.Join(t.TempDir(), "Folder With Spaces")
494 if err := os.MkdirAll(filepath.Join(outside, "sub"), 0o755); err != nil {
495 t.Fatal(err)
496 }
497 if err := os.WriteFile(filepath.Join(outside, "sub", "notes.txt"), []byte("notes"), 0o644); err != nil {
498 t.Fatal(err)
499 }
500 expectedOutside := outside
501 if resolved, err := filepath.EvalSymlinks(outside); err == nil {
502 expectedOutside = resolved
503 }
504 expectedDisplayPath := filepath.ToSlash(expectedOutside)
505 if err := os.Chdir(workspace); err != nil {
506 t.Fatal(err)
507 }
508
509 ctrl := control.New(control.Options{WorkspaceRoot: workspace})
510 app := &App{
511 tabs: map[string]*WorkspaceTab{
512 "project": {ID: "project", WorkspaceRoot: workspace, Ctrl: ctrl},
513 },
514 activeTabID: "project",
515 }
516
517 got, err := app.AttachDropped(outside)
518 if err != nil {
519 t.Fatalf("AttachDropped: %v", err)
520 }
521 if got.Kind != "workspace" || !got.IsDir {
522 t.Fatalf("got %+v, want workspace directory ref", got)
523 }
524 if !strings.HasPrefix(got.Path, "__reasonix_external_folder/") || strings.ContainsAny(got.Path, " \t\r\n") {
525 t.Fatalf("external folder path token = %q, want whitespace-free external token", got.Path)
526 }
527 if got.DisplayPath != expectedDisplayPath {
528 t.Fatalf("display path = %q, want %q", got.DisplayPath, expectedDisplayPath)
529 }
530
531 block, errs := ctrl.ResolveScopedRefs(context.Background(), "inspect @"+got.Path+"/")
532 if len(errs) != 0 {
533 t.Fatalf("ResolveScopedRefs errors = %v", errs)
534 }
535 if !strings.Contains(block, `<dir path="`+expectedDisplayPath+`">`) ||
536 !strings.Contains(block, "sub/") ||
537 !strings.Contains(block, "sub/notes.txt") {
538 t.Fatalf("external dropped folder should resolve as dir context:\n%s", block)
539 }
540 }
541
542 func TestAttachDroppedOutsideWorkspaceDirRegistersAfterPinnedOwnerRebuild(t *testing.T) {
543 isolateDesktopUserDirs(t)
544 setDesktopTestCredential(t, "TEST_MODEL_KEY", "sk-test")
545 cfg := config.Default()
546 cfg.DefaultModel = "test/test-model"
547 cfg.Desktop.ProviderAccess = []string{"test"}
548 cfg.Providers = []config.ProviderEntry{
549 {Name: "test", Kind: "openai", BaseURL: "https://example.invalid/v1", Model: "test-model", APIKeyEnv: "TEST_MODEL_KEY"},
550 }
551 if err := cfg.SaveTo(config.UserConfigPath()); err != nil {
552 t.Fatalf("save config: %v", err)
553 }
554 orig, _ := os.Getwd()
555 defer os.Chdir(orig)
556
557 projectA := t.TempDir()
558 projectB := t.TempDir()
559 outside := filepath.Join(t.TempDir(), "External")
560 if err := os.MkdirAll(filepath.Join(outside, "sub"), 0o755); err != nil {
561 t.Fatal(err)
562 }
563 if err := os.WriteFile(filepath.Join(outside, "sub", "notes.txt"), []byte("notes"), 0o644); err != nil {
564 t.Fatal(err)
565 }
566 if err := addProject(projectA, "Project A"); err != nil {
567 t.Fatalf("add project A: %v", err)
568 }
569 if err := addProject(projectB, "Project B"); err != nil {
570 t.Fatalf("add project B: %v", err)
571 }
572 sessionDirA := desktopSessionDir(projectA)
573 sessionDirB := desktopSessionDir(projectB)
574 if err := os.MkdirAll(sessionDirA, 0o755); err != nil {
575 t.Fatalf("mkdir project A sessions: %v", err)
576 }
577 if err := os.MkdirAll(sessionDirB, 0o755); err != nil {
578 t.Fatalf("mkdir project B sessions: %v", err)
579 }
580 sessionPathA := writeTopicSessionWithPrompt(t, sessionDirA, "project-a.jsonl", "topic_external_ref", "External ref", projectA, "project A prompt", time.Now())
581 sessionPathB := filepath.Join(sessionDirB, "wrong.jsonl")
582 oldCtrl := control.New(control.Options{
583 SessionDir: sessionDirB,
584 SessionPath: sessionPathB,
585 WorkspaceRoot: projectB,
586 Sink: event.Discard,
587 })
588 app := NewApp()
589 app.readyHook = func() {}
590 tab := &WorkspaceTab{
591 ID: "project",
592 Scope: "project",
593 WorkspaceRoot: projectB,
594 TopicID: "topic_external_ref",
595 TopicTitle: "External ref",
596 SessionPath: sessionPathA,
597 Ready: true,
598 model: "test/test-model",
599 Ctrl: oldCtrl,
600 sink: &tabEventSink{tabID: "project", app: app},
601 disabledMCP: map[string]ServerView{},
602 }
603 app.tabs = map[string]*WorkspaceTab{tab.ID: tab}
604 app.tabOrder = []string{tab.ID}
605 app.activeTabID = tab.ID
606 t.Cleanup(func() {
607 if tab.Ctrl != nil {
608 tab.Ctrl.Close()
609 }
610 })
611
612 got, err := app.AttachDropped(outside)
613 if err != nil {
614 t.Fatalf("AttachDropped: %v", err)
615 }
616 if tab.Ctrl == oldCtrl {
617 t.Fatal("stale controller was reused for external folder ref")
618 }
619 if gotRoot := normalizeProjectRoot(tab.Ctrl.WorkspaceRoot()); gotRoot != normalizeProjectRoot(projectA) {
620 t.Fatalf("controller workspace root = %q, want project A %q", gotRoot, normalizeProjectRoot(projectA))
621 }
622 resolver, ok := tab.Ctrl.(interface {
623 ResolveScopedRefs(context.Context, string) (string, []string)
624 })
625 if !ok {
626 t.Fatalf("rebuilt controller does not resolve scoped refs: %T", tab.Ctrl)
627 }
628 block, errs := resolver.ResolveScopedRefs(context.Background(), "inspect @"+got.Path+"/")
629 if len(errs) != 0 {
630 t.Fatalf("ResolveScopedRefs errors = %v", errs)
631 }
632 if !strings.Contains(block, "sub/notes.txt") {
633 t.Fatalf("external dropped folder should resolve on rebuilt controller:\n%s", block)
634 }
635 }
636
636 lines GO