ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/t/12_vim_startup.t
Revision: 1.14
Committed: Sat Nov 7 20:37:04 2026 UTC (11 days, 2 hours ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 use common::sense;
2     use Test::More;
3     use File::Temp ();
4     use File::Path ();
5     use AnyEvent::MCP::Config ();
6     use IPC::Open3 ();
7     use Symbol ();
8    
9     my $dir = File::Temp::tempdir (CLEANUP => 1);
10     File::Path::mkpath (["$dir/home", "$dir/config"], 0, 0700);
11     local $ENV{HOME} = "$dir/home";
12     local $ENV{XDG_CONFIG_HOME} = "$dir/config";
13    
14     sub run_vim {
15     my @argv = @_;
16     my $error = Symbol::gensym;
17     my $pid = IPC::Open3::open3 (my $input, my $output, $error,
18     $^X, "-Ilib", "bin/mcp-vim", @argv);
19     close $input;
20     local $/;
21     my $out = <$output> // "";
22     my $err = <$error> // "";
23     waitpid $pid, 0;
24     ($? >> 8, $out, $err)
25     }
26    
27     my ($status, $url, $err) = run_vim ("--print-url");
28     is $status, 0, "print-url exits successfully";
29 root 1.13 like $url, qr{\Ahttp://127\.0\.0\.1:[0-9]+/[A-Za-z0-9_-]{43}/ \# user global:default \(connect\)\n\z},
30 root 1.1 "print-url emits a cookie-bearing URL prefix";
31     is $err, "", "print-url has no diagnostics on success";
32    
33     my $file = "$dir/config/anyevent-mcp/global.ini";
34     ok -f $file && !((stat $file)[2] & 077), "global master secret saved privately";
35     my $config = AnyEvent::MCP::Config->new (file => $file);
36 root 1.8 my ($default_port, $cookie);
37     {
38     local $0 = "mcp-vim";
39     $default_port = $config->server_port ("default");
40 root 1.13 $cookie = $config->user_credentials ("default")->[0][2];
41 root 1.8 }
42 root 1.2 like $url, qr{\Ahttp://127\.0\.0\.1:$default_port/},
43     "default port is derived from the service name";
44 root 1.8 # token is derived in the broker program domain above.
45 root 1.13 like $url, qr{\Q/$cookie/\E \# user global:default \(connect\)\n\z}, "cookie derives from global master secret";
46 root 1.1
47     my ($again_status, $again_url) = run_vim ("--print-url");
48     is $again_status, 0, "second invocation exits successfully";
49     is $again_url, $url, "cookie and URL are stable across processes";
50 root 1.3 ($status, $url) = run_vim ("--print-url", "--token", "cli-first", "--token", "cli-second");
51     is $status, 0, "repeated --token options accepted";
52     like $url, qr{/cli-first/\n\z}, "first CLI token is advertised";
53 root 1.1 ok !-e "$dir/home/.xpcse-broker", "startup no longer generates old cookie config";
54    
55 root 1.2 open my $legacy, ">>", $file or die $!;
56 root 1.8 print {$legacy} "\n[instance default]\nbind = 127.0.0.1:31749\n";
57 root 1.1 close $legacy or die $!;
58     ($status, $url, $err) = run_vim ("--print-url");
59 root 1.2 is $status, 0, "named bind setting accepted";
60 root 1.13 is $url, "http://127.0.0.1:31749/$cookie/ # user global:default (connect)\n",
61 root 1.2 "named bind overrides derived port";
62 root 1.1
63     open my $fh, ">>", $file or die $!;
64 root 1.3 print {$fh} "tokens = explicit-cookie,secondary-token\n";
65 root 1.1 close $fh or die $!;
66     ($status, $url) = run_vim ("--print-url");
67 root 1.3 is $status, 0, "explicit configured tokens accepted";
68 root 1.1 like $url, qr{\Ahttp://[^:/\s]+:31749/explicit-cookie/\n\z},
69 root 1.3 "first configured token overrides derived default";
70 root 1.1
71 root 1.2 ($status, $url) = run_vim ("--print-url", "--bind", "127.0.0.1:41859");
72     is $status, 0, "bind switch accepted";
73 root 1.1 is $url, "http://127.0.0.1:41859/explicit-cookie/\n",
74 root 1.2 "explicit bind overrides named setting";
75 root 1.1
76 root 1.2 ($status, $url) = run_vim ("--print-url", "--bind", "[::1]:41859");
77     is $status, 0, "IPv6 bind accepted";
78 root 1.1 is $url, "http://[::1]:41859/explicit-cookie/\n",
79     "IPv6 URL host is bracketed";
80    
81 root 1.2 ($status, $url) = run_vim ("--print-url", "--bind", "[::]");
82 root 1.1 is $status, 0, "IPv6 wildcard accepted";
83     like $url, qr{\Ahttp://[^:/\s]+:31749/explicit-cookie/\n\z},
84     "wildcard bind advertises concrete hostname";
85    
86 root 1.2 ($status, undef, $err) = run_vim ("--print-url", "--bind", ":0");
87     ok $status != 0 && $err =~ /port is not known/,
88 root 1.1 "zero port rejected because a printable fixed URL is required";
89 root 1.2 ($status, undef, $err) = run_vim ("--print-url", "--bind", ":65536");
90     ok $status != 0 && $err =~ /Invalid MCP bind/,
91 root 1.1 "out-of-range port rejected";
92 root 1.2 ($status, undef, $err) = run_vim ("--print-url", "--bind", "");
93     ok $status != 0 && $err =~ /Invalid MCP bind/,
94 root 1.1 "empty host rejected";
95    
96     {
97     local $ENV{XDG_CONFIG_HOME} = "/proc/self/mcp-vim-no-persistent-secret-$$";
98     ($status, $url, $err) = run_vim ("--print-url");
99     ok $status != 0 && !length $url,
100     "cannot print a URL backed only by unsaved, process-local entropy";
101     like $err, qr/master secret is not persistently stored/,
102     "unstable master secret is diagnosed explicitly";
103     }
104    
105     ($status, my $help) = run_vim ("--help");
106     is $status, 0, "help exits successfully";
107     like $help, qr/print-url/, "help explains URL switch";
108     ($status, undef, $err) = run_vim ("--not-a-switch");
109     ok $status != 0, "unknown option rejected";
110 root 1.6 # exercise message capture and error propagation in Vim itself, when available.
111     SKIP: {
112 root 1.9 skip "extended real Vim tests (PERL_ANYEVENT_MCP_TESTS=1)", 2
113     unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;
114 root 1.6 my ($vim) = grep -x $_ && !-d _, map "$_/vim", split /:/, $ENV{PATH};
115     skip "vim not available", 2 unless $vim;
116    
117     my $script = "$dir/ex-test.vim";
118     open my $fh, ">", $script or die "$script: $!";
119     print {$fh} <<'VIM';
120 root 1.9 if !exists ('*getscriptinfo') || !exists ('*assert_equal') || !exists ('*bufadd') || !exists ('*bufload') || !exists ('*execute')
121     call writefile (['SKIP: Vim lacks required scripting functions'], argv (0))
122     qa!
123     endif
124 root 1.6 source vim/plugin/mcp-vim.vim
125     let sid = filter (getscriptinfo (), 'v:val.name =~# "mcp-vim\\.vim$"')[0].sid
126     let Rpc = function ('<SNR>' . sid . '_rpc_call_ex')
127     call assert_equal ("\nhello\nworld", Rpc ("echo 'hello' | echo 'world'").output)
128     call assert_equal ('', Rpc ("let g:mcp_ex_test = 42").output)
129     call assert_equal (42, g:mcp_ex_test)
130     call Rpc ("call setline (1, 'changed')\nlet g:mcp_ex_second = 1")
131     call assert_equal ('changed', getline (1))
132     call assert_equal (1, g:mcp_ex_second)
133     let error = ''
134     try
135     call Rpc ("let g:mcp_ex_partial = 1 | throw 'deliberate failure'")
136     catch
137     let error = v:exception
138     endtry
139     call assert_equal ('deliberate failure', error)
140     call assert_equal (1, g:mcp_ex_partial)
141     let error = ''
142     try
143     call Rpc ('NoSuchExCommand')
144     catch
145     let error = v:exception
146     endtry
147     call assert_match ('E492:', error)
148     call assert_equal ("\nafter error", Rpc ("echo 'after error'").output)
149 root 1.7
150     let original = bufnr ('%')
151     let file = tempname ()
152     call writefile (['original'], file)
153     let target = bufadd (file)
154     call bufload (target)
155     call setbufline (target, 1, 'saved by update')
156     let tick = getbufvar (target, 'changedtick')
157     call Rpc ('update', target, tick)
158     call assert_equal (target, bufnr ('%'))
159     call assert_equal (['saved by update'], readfile (file))
160     call assert_equal (0, &modified)
161     call Rpc ("call setline (1, 'newer contents')", target)
162     let error = ''
163     try
164     call Rpc ('let g:mcp_stale_ex_ran = 1', target, tick)
165     catch
166     let error = v:exception
167     endtry
168     call assert_match ('rpc/stale_buffer:', error)
169     call assert_equal (0, exists ('g:mcp_stale_ex_ran'))
170     call assert_equal (['saved by update'], readfile (file))
171     let error = ''
172     try
173     call Rpc ('let g:mcp_stale_ex_ran = 1', v:null, b:changedtick)
174     catch
175     let error = v:exception
176     endtry
177     call assert_match ('requires_buffer', error)
178     call assert_equal (0, exists ('g:mcp_stale_ex_ran'))
179    
180     " Entering a buffer can run autocommands; check the tick afterwards.
181     execute 'hide buffer ' . original
182     execute 'autocmd BufEnter <buffer=' . target . '> call setline (1, "changed on entry")'
183     let tick = getbufvar (target, 'changedtick')
184     let error = ''
185     try
186     call Rpc ('let g:mcp_stale_ex_ran = 1', target, tick)
187     catch
188     let error = v:exception
189     endtry
190     call assert_match ('rpc/stale_buffer:', error)
191     call assert_equal (0, exists ('g:mcp_stale_ex_ran'))
192 root 1.10
193     " A hidden buffer has its own history; undo must not affect the current one.
194     let Undo = function ('<SNR>' . sid . '_rpc_call_undo')
195     let Redo = function ('<SNR>' . sid . '_rpc_call_redo')
196     let Substitute = function ('<SNR>' . sid . '_rpc_call_substitute')
197     let caller = bufnr ('%')
198     let caller_lines = getline (1, '$')
199     let caller_view = winsaveview ()
200     let undo_file = tempname ()
201     call writefile (['base'], undo_file)
202     let undo_buf = bufadd (undo_file)
203     call bufload (undo_buf)
204     call Substitute (undo_buf, '%', 'base', 'one', '', getbufvar (undo_buf, 'changedtick'))
205     let one = getbufvar (undo_buf, 'changedtick')
206     call Substitute (undo_buf, '%', 'one', 'two', '', one)
207     let two = getbufvar (undo_buf, 'changedtick')
208     let result = Undo (undo_buf, two)
209     call assert_equal (['one'], getbufline (undo_buf, 1, '$'))
210     call assert_equal (undo_buf, result.buffer)
211     call assert_equal (1, result.changed)
212     call assert_equal (1, result.modified)
213     call assert_equal (1, result.seq_cur)
214     call assert_equal (2, result.seq_last)
215     call assert_equal (getbufvar (undo_buf, 'changedtick'), result.changedtick)
216     call assert_equal (caller, bufnr ('%'))
217     call assert_equal (caller_lines, getline (1, '$'))
218     call assert_equal (caller_view, winsaveview ())
219     call assert_equal (['base'], readfile (undo_file))
220    
221     let error = ''
222     try
223     call Redo (undo_buf, two)
224     catch
225     let error = v:exception
226     endtry
227     call assert_match ('rpc/stale_buffer:', error)
228     call assert_equal (['one'], getbufline (undo_buf, 1, '$'))
229     let result = Redo (undo_buf, result.changedtick)
230     call assert_equal (['two'], getbufline (undo_buf, 1, '$'))
231     call assert_equal (2, result.seq_cur)
232     call assert_equal (1, result.changed)
233     let result = Redo (undo_buf, result.changedtick)
234     call assert_equal (0, result.changed)
235     call assert_equal (2, result.seq_cur)
236    
237     let result = Undo (undo_buf, result.changedtick)
238     let result = Undo (undo_buf, result.changedtick)
239     call assert_equal (['base'], getbufline (undo_buf, 1, '$'))
240     call assert_equal (0, result.seq_cur)
241     call assert_equal (0, result.modified)
242     let result = Undo (undo_buf, result.changedtick)
243     call assert_equal (0, result.changed)
244     call assert_equal (0, result.seq_cur)
245    
246     " A new edit after undo selects a new branch; redo cannot resurrect the old one.
247     let result = Redo (undo_buf, result.changedtick)
248     call Substitute (undo_buf, '%', 'one', 'branch', '', result.changedtick)
249     let tick = getbufvar (undo_buf, 'changedtick')
250     let result = Redo (undo_buf, tick)
251     call assert_equal (['branch'], getbufline (undo_buf, 1, '$'))
252     call assert_equal (0, result.changed)
253     call assert_equal (3, result.seq_cur)
254     call assert_equal (3, result.seq_last)
255    
256     call setbufvar (undo_buf, '&modifiable', 0)
257     let error = ''
258     try
259     call Undo (undo_buf, result.changedtick)
260     catch
261     let error = v:exception
262     endtry
263     call assert_match ('rpc/not_modifiable', error)
264     call setbufvar (undo_buf, '&modifiable', 1)
265     call assert_equal (['base'], readfile (undo_file))
266     call assert_equal (caller, bufnr ('%'))
267    
268     " The current-buffer case keeps Vim's normal cursor adjustment and history.
269     execute 'noautocmd hide buffer ' . undo_buf
270     let result = Undo (undo_buf, getbufvar (undo_buf, 'changedtick'))
271     call assert_equal (undo_buf, bufnr ('%'))
272     call assert_equal (['one'], getline (1, '$'))
273     call assert_equal (1, result.seq_cur)
274     let result = Redo (undo_buf, result.changedtick)
275     call assert_equal (['branch'], getline (1, '$'))
276     call assert_equal (3, result.seq_cur)
277 root 1.11
278     let Update = function ('<SNR>' . sid . '_rpc_call_update')
279     let caller = bufnr ('%')
280     let caller_lines = getline (1, '$')
281     let caller_view = winsaveview ()
282     let update_file = tempname ()
283     call writefile (['saved'], update_file)
284     let update_buf = bufadd (update_file)
285     call bufload (update_buf)
286     call setbufline (update_buf, 1, 'edited')
287     let old_tick = getbufvar (update_buf, 'changedtick')
288     let result = Update (update_buf, old_tick)
289     call assert_equal (['edited'], readfile (update_file))
290     call assert_equal (0, result.modified)
291     call assert_equal (update_buf, result.buffer)
292     call assert_equal (update_file, result.filename)
293     call assert_equal (getbufvar (update_buf, 'changedtick'), result.changedtick)
294     call assert_equal (caller, bufnr ('%'))
295     call assert_equal (caller_lines, getline (1, '$'))
296     call assert_equal (caller_view, winsaveview ())
297    
298     let g:update_writes = 0
299     execute 'autocmd BufWritePre <buffer=' . update_buf . '> let g:update_writes += 1'
300     let result = Update (update_buf, result.changedtick)
301     call assert_equal (0, g:update_writes)
302     call assert_equal (0, result.modified)
303     call setbufline (update_buf, 1, 'newer')
304     let error = ''
305     try
306     call Update (update_buf, old_tick)
307     catch
308     let error = v:exception
309     endtry
310     call assert_match ('rpc/stale_buffer:', error)
311     call assert_equal (['edited'], readfile (update_file))
312     call assert_equal (0, g:update_writes)
313    
314     " Write autocommands run normally; the result describes the buffer afterwards.
315     execute 'autocmd BufWritePre <buffer=' . update_buf . '> call setline (1, "prewrite")'
316     execute 'autocmd BufWritePost <buffer=' . update_buf . '> call setline (1, "postwrite")'
317     let result = Update (update_buf, getbufvar (update_buf, 'changedtick'))
318     call assert_equal (1, g:update_writes)
319     call assert_equal (['prewrite'], readfile (update_file))
320     call assert_equal (['postwrite'], getbufline (update_buf, 1, '$'))
321     call assert_equal (getbufvar (update_buf, '&modified'), result.modified)
322     call assert_equal (getbufvar (update_buf, 'changedtick'), result.changedtick)
323     execute 'autocmd! BufWritePre <buffer=' . update_buf . '>'
324     execute 'autocmd! BufWritePost <buffer=' . update_buf . '>'
325    
326     call setbufvar (update_buf, '&modified', 1)
327     execute 'autocmd BufWritePre <buffer=' . update_buf . '> throw "deliberate write failure"'
328     let error = ''
329     try
330     call Update (update_buf, result.changedtick)
331     catch
332     let error = v:exception
333     endtry
334     call assert_equal ('deliberate write failure', error)
335     call assert_equal (['prewrite'], readfile (update_file))
336     call assert_equal (1, getbufvar (update_buf, '&modified'))
337     call assert_equal (caller, bufnr ('%'))
338     execute 'autocmd! BufWritePre <buffer=' . update_buf . '>'
339    
340     call setbufvar (update_buf, '&readonly', 1)
341     let error = ''
342     try
343     call Update (update_buf, getbufvar (update_buf, 'changedtick'))
344     catch
345     let error = v:exception
346     endtry
347     call assert_match ('E45:', error)
348     call assert_equal (['prewrite'], readfile (update_file))
349     call setbufvar (update_buf, '&readonly', 0)
350     let result = Update (update_buf, getbufvar (update_buf, 'changedtick'))
351     call assert_equal (['postwrite'], readfile (update_file))
352     call assert_equal (0, result.modified)
353    
354     " A write autocommand may change the current buffer; report the saved target.
355     execute 'autocmd BufWritePost <buffer=' . update_buf . '> execute "noautocmd hide buffer " . ' . caller
356     call setbufline (update_buf, 1, 'switched')
357     let result = Update (update_buf, getbufvar (update_buf, 'changedtick'))
358     call assert_equal (update_buf, result.buffer)
359     call assert_equal (update_file, result.filename)
360     call assert_equal (getbufvar (update_buf, 'changedtick'), result.changedtick)
361     call assert_equal (['switched'], readfile (update_file))
362     call assert_equal (caller, bufnr ('%'))
363     execute 'autocmd! BufWritePost <buffer=' . update_buf . '>'
364    
365     let unnamed = bufadd ('')
366     call bufload (unnamed)
367     call setbufline (unnamed, 1, 'unsaved')
368     let error = ''
369     try
370     call Update (unnamed, getbufvar (unnamed, 'changedtick'))
371     catch
372     let error = v:exception
373     endtry
374     call assert_match ('E32:', error)
375     call assert_equal (1, getbufvar (unnamed, '&modified'))
376     call assert_equal (caller, bufnr ('%'))
377 root 1.12 " Opening files loads a listed buffer without changing the current view.
378     let Open = function ('<SNR>' . sid . '_rpc_call_open')
379     let open_dir = tempname ()
380     call mkdir (open_dir)
381     let old_cwd = getcwd ()
382     execute 'lcd ' . fnameescape (open_dir)
383     let caller = bufnr ('%')
384     let caller_lines = getline (1, '$')
385     let caller_view = winsaveview ()
386     let literal_name = "literal # % | [*] $HOME 'quote'.txt"
387     call writefile (['disk one', 'disk two'], literal_name)
388     let opened = Open (literal_name)
389     call assert_equal (open_dir . '/' . literal_name, opened.filename)
390     call assert_equal (['disk one', 'disk two'], getbufline (opened.buffer, 1, '$'))
391     call assert_equal (1, buflisted (opened.buffer))
392     call assert_equal (2, opened.line_count)
393     call assert_equal (0, opened.modified)
394     call assert_equal (getbufvar (opened.buffer, 'changedtick'), opened.changedtick)
395     call assert_equal (caller, bufnr ('%'))
396     call assert_equal (caller_lines, getline (1, '$'))
397     call assert_equal (caller_view, winsaveview ())
398    
399     call setbufline (opened.buffer, 1, 'unsaved edit')
400     call writefile (['external disk edit'], literal_name)
401     let reopened = Open (open_dir . '/' . literal_name)
402     call assert_equal (opened.buffer, reopened.buffer)
403     call assert_equal (['unsaved edit', 'disk two'], getbufline (reopened.buffer, 1, '$'))
404     call assert_equal (1, reopened.modified)
405     call assert_equal (getbufvar (reopened.buffer, 'changedtick'), reopened.changedtick)
406    
407     call writefile (['unloaded'], 'unloaded.txt')
408     let unloaded = bufadd ('unloaded.txt')
409     call assert_equal (0, bufloaded (unloaded))
410     let loaded = Open ('unloaded.txt')
411     call assert_equal (unloaded, loaded.buffer)
412     call assert_equal (1, bufloaded (loaded.buffer))
413     call assert_equal (1, buflisted (loaded.buffer))
414     call assert_equal (['unloaded'], getbufline (loaded.buffer, 1, '$'))
415    
416     let missing = Open ('missing.txt')
417     call assert_equal ([''], getbufline (missing.buffer, 1, '$'))
418     call assert_equal (1, missing.line_count)
419     call assert_equal (0, missing.modified)
420     call assert_equal (0, filereadable ('missing.txt'))
421    
422     let g:open_event_file = open_dir . '/events.txt'
423     call writefile (['disk'], g:open_event_file)
424     augroup MCP_TEST_OPEN
425     autocmd!
426     autocmd BufReadPost * if expand ('<afile>:p') ==# g:open_event_file | call setline (1, 'autocommand edit') | endif
427     augroup END
428     let event_buffer = Open (g:open_event_file)
429     call assert_equal (['autocommand edit'], getbufline (event_buffer.buffer, 1, '$'))
430     call assert_equal (getbufvar (event_buffer.buffer, 'changedtick'), event_buffer.changedtick)
431     call assert_equal (getbufvar (event_buffer.buffer, '&modified'), event_buffer.modified)
432     call assert_equal (caller, bufnr ('%'))
433    
434     set modeline modelines=5
435     call writefile (['text', 'vim: set ts=7:'], 'modeline.txt')
436     let modeline_buffer = Open ('modeline.txt')
437     call assert_equal (7, getbufvar (modeline_buffer.buffer, '&tabstop'))
438    
439     let g:open_event_file = open_dir . '/failure.txt'
440     call writefile (['disk'], g:open_event_file)
441     augroup MCP_TEST_OPEN
442     autocmd!
443     autocmd BufReadPost * if expand ('<afile>:p') ==# g:open_event_file | throw 'deliberate open failure' | endif
444     augroup END
445     let error = ''
446     try
447     call Open (g:open_event_file)
448     catch
449     let error = v:exception
450     endtry
451     call assert_match ('deliberate open failure', error)
452     call assert_equal (caller, bufnr ('%'))
453     augroup MCP_TEST_OPEN
454     autocmd!
455     augroup END
456     execute 'lcd ' . fnameescape (old_cwd)
457     call delete (open_dir, 'rf')
458 root 1.11 call delete (update_file)
459 root 1.10 call delete (undo_file)
460 root 1.11 call delete (file)
461 root 1.6 call writefile (v:errors, argv (0))
462     qa!
463     VIM
464     close $fh or die "$script: $!";
465     my $errors = "$dir/vim-errors";
466     my $error = Symbol::gensym;
467     my $pid = IPC::Open3::open3 (my $input, my $output, $error,
468     $vim, "-Nu", "NONE", "-i", "NONE", "-n", "-es", "-S", $script, "--", $errors);
469     close $input;
470     local $/;
471     my $stdout = <$output>;
472     my $stderr = <$error>;
473     waitpid $pid, 0;
474 root 1.9 my $status = $?;
475 root 1.6 open $fh, "<", $errors or die "$errors: $!";
476 root 1.9 my $messages = <$fh> // "";
477 root 1.6 close $fh;
478 root 1.9
479     skip $messages, 2 if !$status && $messages =~ /\ASKIP:/;
480     is $status, 0, "real Vim loads the plugin and exercises unrestricted Ex RPC";
481     diag $stdout, $stderr if $status;
482     is $messages, "", "real Vim captures messages, changes state, propagates errors and retains partial effects";
483 root 1.6 }
484    
485 root 1.1 done_testing;