1
+ import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
2
+ import { spawn , ChildProcess } from 'child_process' ;
3
+ import { fileURLToPath } from 'url' ;
4
+ import { dirname , join } from 'path' ;
5
+
6
+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
7
+
8
+ describe ( 'Build artifact integration tests' , ( ) => {
9
+ let serverProcess : ChildProcess ;
10
+ let serverOutput : string = '' ;
11
+ let serverError : string = '' ;
12
+
13
+ beforeAll ( async ( ) => {
14
+ // Build the project first
15
+ await new Promise ( ( resolve , reject ) => {
16
+ const buildProcess = spawn ( 'yarn' , [ 'build' ] , {
17
+ cwd : join ( __dirname , '..' ) ,
18
+ shell : true ,
19
+ } ) ;
20
+
21
+ buildProcess . on ( 'close' , ( code ) => {
22
+ if ( code === 0 ) {
23
+ resolve ( undefined ) ;
24
+ } else {
25
+ reject ( new Error ( `Build failed with code ${ code } ` ) ) ;
26
+ }
27
+ } ) ;
28
+ } ) ;
29
+
30
+ // Start the server
31
+ serverProcess = spawn ( 'node' , [ join ( __dirname , '../dist/index.js' ) ] , {
32
+ stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
33
+ } ) ;
34
+
35
+ // Capture output
36
+ serverProcess . stdout ?. on ( 'data' , ( data ) => {
37
+ serverOutput += data . toString ( ) ;
38
+ } ) ;
39
+
40
+ serverProcess . stderr ?. on ( 'data' , ( data ) => {
41
+ serverError += data . toString ( ) ;
42
+ } ) ;
43
+
44
+ // Wait for server to start
45
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
46
+ } ) ;
47
+
48
+ afterAll ( ( ) => {
49
+ if ( serverProcess ) {
50
+ serverProcess . kill ( ) ;
51
+ }
52
+ } ) ;
53
+
54
+ it ( 'should start without errors' , ( ) => {
55
+ expect ( serverError ) . toContain ( 'MCP Wayback Machine server running on stdio' ) ;
56
+ expect ( serverProcess . killed ) . toBe ( false ) ;
57
+ } ) ;
58
+
59
+ it ( 'should respond to list_tools request' , async ( ) => {
60
+ const request = {
61
+ jsonrpc : '2.0' ,
62
+ method : 'tools/list' ,
63
+ params : { } ,
64
+ id : 1 ,
65
+ } ;
66
+
67
+ // Send request
68
+ serverProcess . stdin ?. write ( JSON . stringify ( request ) + '\n' ) ;
69
+
70
+ // Wait for response
71
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
72
+
73
+ // The response should be in stdout (not stderr)
74
+ expect ( serverOutput . length ) . toBeGreaterThan ( 0 ) ;
75
+ } ) ;
76
+
77
+ it ( 'should handle malformed requests gracefully' , async ( ) => {
78
+ const malformedRequest = 'not json' ;
79
+
80
+ serverProcess . stdin ?. write ( malformedRequest + '\n' ) ;
81
+
82
+ // Wait for potential error handling
83
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
84
+
85
+ // Server should still be running
86
+ expect ( serverProcess . killed ) . toBe ( false ) ;
87
+ } ) ;
88
+ } ) ;
89
+
90
+ describe ( 'Executable permissions' , ( ) => {
91
+ it ( 'should have shebang in built file' , async ( ) => {
92
+ const fs = await import ( 'fs/promises' ) ;
93
+ const builtFile = join ( __dirname , '../dist/index.js' ) ;
94
+
95
+ const content = await fs . readFile ( builtFile , 'utf-8' ) ;
96
+ expect ( content ) . toMatch ( / ^ # ! / ) ;
97
+ expect ( content ) . toContain ( '#!/usr/bin/env node' ) ;
98
+ } ) ;
99
+ } ) ;
0 commit comments