88 "net/http/httptest"
99 "os"
1010 "path/filepath"
11+ "strings"
1112 "testing"
1213)
1314
@@ -224,15 +225,8 @@ func tarGz(t *testing.T, name string, body []byte) []byte {
224225 return b
225226}
226227
227- func TestInstall_TarballExtract (t * testing.T ) {
228- setRealms (t )
229- paths , err := Resolve ()
230- if err != nil {
231- t .Fatalf ("Resolve: %v" , err )
232- }
233-
234- runedTar := tarGz (t , filepath .Base (paths .RunedBinary ), []byte ("RUNED" ))
235- mcpTar := tarGz (t , filepath .Base (paths .RuneMCPBinary ), []byte ("RUNE-MCP" ))
228+ func tarballManifestServer (t * testing.T , runedTar , mcpTar []byte ) string {
229+ t .Helper ()
236230
237231 var srv * httptest.Server
238232 mux := http .NewServeMux ()
@@ -258,7 +252,20 @@ func TestInstall_TarballExtract(t *testing.T) {
258252 srv = httptest .NewServer (mux )
259253 t .Cleanup (srv .Close )
260254
261- r , err := Install (context .Background (), InstallOptions {ManifestURL : srv .URL + "/manifest.json" })
255+ return srv .URL + "/manifest.json"
256+ }
257+
258+ func TestInstall_TarballExtract (t * testing.T ) {
259+ setRealms (t )
260+ paths , err := Resolve ()
261+ if err != nil {
262+ t .Fatalf ("Resolve: %v" , err )
263+ }
264+
265+ runedTar := tarGz (t , filepath .Base (paths .RunedBinary ), []byte ("RUNED" ))
266+ mcpTar := tarGz (t , filepath .Base (paths .RuneMCPBinary ), []byte ("RUNE-MCP" ))
267+
268+ r , err := Install (context .Background (), InstallOptions {ManifestURL : tarballManifestServer (t , runedTar , mcpTar )})
262269 if err != nil {
263270 t .Fatalf ("Install (tar.gz): %v" , err )
264271 }
@@ -277,3 +284,59 @@ func TestInstall_TarballExtract(t *testing.T) {
277284 }
278285 }
279286}
287+
288+ func TestInstall_TarballMissingExpectedFile (t * testing.T ) {
289+ setRealms (t )
290+ paths , err := Resolve ()
291+ if err != nil {
292+ t .Fatalf ("Resolve: %v" , err )
293+ }
294+
295+ runedTar := tarGz (t , filepath .Base (paths .RunedBinary ), []byte ("RUNED" ))
296+ mcpTar := tarGz (t , "not-rune-mcp" , []byte ("WRONG-NAME" )) // valid archive but wrong entry
297+
298+ r , err := Install (context .Background (), InstallOptions {ManifestURL : tarballManifestServer (t , runedTar , mcpTar )})
299+ if err == nil {
300+ t .Fatal ("expected error when the tarball lacks the expected file" )
301+ }
302+ if ! strings .Contains (err .Error (), "did not have expected file" ) {
303+ t .Errorf ("err = %v, want 'did not have expected file'" , err )
304+ }
305+ if r .Status != "partial" {
306+ t .Errorf ("Status=%q, want partial" , r .Status )
307+ }
308+ if r .Failed [StepRuneMCP ] == "" {
309+ t .Errorf ("Failed missing %s: %+v" , StepRuneMCP , r .Failed )
310+ }
311+ if fileExists (paths .RuneMCPBinary ) {
312+ t .Errorf ("%s should not exist when the tarball lacks it" , paths .RuneMCPBinary )
313+ }
314+ }
315+
316+ func TestInstall_CorruptTarball (t * testing.T ) {
317+ setRealms (t )
318+ paths , err := Resolve ()
319+ if err != nil {
320+ t .Fatalf ("Resolve: %v" , err )
321+ }
322+
323+ runedTar := tarGz (t , filepath .Base (paths .RunedBinary ), []byte ("RUNED" ))
324+ corrupt := []byte ("this is not a gzip stream" ) // SHA and size are matched but broken tarball
325+
326+ r , err := Install (context .Background (), InstallOptions {ManifestURL : tarballManifestServer (t , runedTar , corrupt )})
327+ if err == nil {
328+ t .Fatal ("expected extract error for a corrupt gzip body" )
329+ }
330+ if ! strings .Contains (err .Error (), "extract" ) {
331+ t .Errorf ("err = %v, want an extract failure" , err )
332+ }
333+ if r .Status != "partial" {
334+ t .Errorf ("Status=%q, want partial" , r .Status )
335+ }
336+ if r .Failed [StepRuneMCP ] == "" {
337+ t .Errorf ("Failed missing %s: %+v" , StepRuneMCP , r .Failed )
338+ }
339+ if fileExists (paths .RuneMCPBinary ) {
340+ t .Errorf ("%s should not exist after a failed extract" , paths .RuneMCPBinary )
341+ }
342+ }
0 commit comments