from test/more import *;

requires_capability( "fs" );

from std/archive import Archive;
from std/io import Path;
from std/zuzuzoo import Zuzuzoo;

function ensure_dir ( path ) {
	return true if path.exists();
	let parent := path.parent();
	ensure_dir(parent) if not parent.exists();
	path.mkdir();
	return true;
}

function root () {
	let dir := Path.tempdir();
	let lib := dir.child("lib");
	let bin := dir.child("bin");
	let meta := dir.child("meta");
	ensure_dir(lib);
	ensure_dir(bin);
	ensure_dir(meta);
	return {
		dir_obj: dir,
		lib_obj: lib,
		lib_dir: lib.to_String(),
		bin_dir: bin.to_String(),
		meta_dir: meta.to_String(),
	};
}

let archive := Path.tempfile();
Archive.dump(
	archive,
	{
		entries: [
			{
				path: "partial-app-1.0.0/zuzu-distribution.json",
				data: to_binary(
					"{\"name\":\"partial-app\",\"version\":\"1.0.0\"," _
					"\"author\":\"Example\",\"license\":\"MIT\"}\n",
				),
			},
			{
				path: "partial-app-1.0.0/modules/partial/app.zzm",
				data: to_binary("// complete module\n"),
			},
		],
	},
	"zip",
);

let target := root();
let zoo := new Zuzuzoo(
	lib_dir: target{lib_dir},
	bin_dir: target{bin_dir},
	meta_dir: target{meta_dir},
	home: Path.tempdir().to_String(),
);
let install := zoo.install( archive.to_String(), { no_test: true } );
ok( install{ok}, "install succeeds" );

target{lib_obj}.child("partial").child("app.zzm").spew_utf8("// trunc\n");
let result := zoo.verify("partial/app");

is( result{ok}, false, "verify fails after truncation" );
is( result{hash_mismatches}.length(), 1, "truncated file has hash mismatch" );
is( result{size_mismatches}.length(), 1, "truncated file has size mismatch" );

done_testing();
