App-FileSync

 view release on metacpan or  search on metacpan

t/sync.t  view on Meta::CPAN

use strict;
use warnings;
use Test::Most;
use Test::Exception;

use File::Path qw(make_path);
use File::Spec;

use lib 't/lib';
use TestSync;
use App::FileSync::Admin;
use App::FileSync::Store;
use App::FileSync::Trash;

my $T0 = 1_700_000_000;

# ---------------------------------------------------------------- basic sync
{
	my $env = TestSync->new_env();

	$env->write_file('client', 'a.txt', "alpha\n", $T0);
	$env->write_file('client', 'sub/b.bin', "\x00\x01\x02\xff", $T0 + 10);
	make_path(File::Spec->catdir($env->{client}, 'empty'));
	utime($T0, $T0, File::Spec->catdir($env->{client}, 'empty'));

	my $result = $env->sync();
	is($result->{summary}{pushed}, 3, 'two files and one empty directory pushed');
	is($result->{exit}, 0, 'a clean sync exits 0');
	is($env->read_file('server', 'a.txt'), "alpha\n", 'file content reached the master');
	ok($env->exists_in('server', 'sub/b.bin'), 'nested file reached the master');
	ok($env->exists_in('server', 'empty'), 'empty directory reached the master');
	is((stat($env->server_path('a.txt')))[9], $T0, 'mtime preserved on the master');

	my $again = $env->sync();
	is($again->{summary}{pushed}, 0, 'second run pushes nothing');
	is($again->{summary}{pulled}, 0, 'second run pulls nothing');
	is($again->{exit}, 0, 'second run is clean');

	# Master-side edit is pulled.
	$env->write_file('server', 'a.txt', "changed on the server\n", $T0 + 100);
	my $pull = $env->sync();
	is($pull->{summary}{pulled}, 1, 'master-side change pulled');
	is($env->read_file('client', 'a.txt'), "changed on the server\n", 'client content updated');

	# Client-side delete goes to the server trash.
	unlink $env->client_path('sub/b.bin');
	my $delete = $env->sync();
	is($delete->{summary}{deleted}, 1, 'client deletion propagated');
	ok(!$env->exists_in('server', 'sub/b.bin'), 'file removed from the master');

	my @trashed = glob(File::Spec->catfile($env->{server}, '.filesync', 'repos', 'docs',
		'trash', '*', '*', '*', 'sub', 'b.bin'));
	ok(@trashed, 'deleted file is in the server trash');
	is(do { local $/; open my $fh, '<', $trashed[0] or die $!; <$fh> }, "\x00\x01\x02\xff",
		'trash keeps the content');

	my @journal = glob(File::Spec->catfile($env->{server}, '.filesync', 'repos', 'docs',
		'journal', '*.jsonl'));
	ok(@journal, 'a journal exists');
	my $journal_text = do { local $/; open my $fh, '<', $journal[0] or die $!; <$fh> };
	like($journal_text, qr/"action":"push"/, 'pushes are journaled');
	like($journal_text, qr/"action":"delete"/, 'deletions are journaled');

	# Master-side delete goes to the client trash.
	unlink $env->server_path('a.txt');
	my $remote_delete = $env->sync();
	is($remote_delete->{summary}{deleted}, 1, 'master deletion propagated');
	ok(!$env->exists_in('client', 'a.txt'), 'client file removed');
	my @client_trash = glob(File::Spec->catfile($env->{home}, 'trash', 'repos', 'docs', '*', '*', '*', 'a.txt'));
	ok(@client_trash, 'client deletion is in the client trash');
}

# ------------------------------------------------- delete versus modification
{
	my $env = TestSync->new_env();

	$env->write_file('client', 'kept.txt', "original\n", $T0);
	$env->write_file('client', 'gone.txt', "original\n", $T0);
	$env->sync();

	# Client deletes gone.txt, the master modifies it: the modification wins.
	unlink $env->client_path('gone.txt');
	$env->write_file('server', 'gone.txt', "edited on the server\n", $T0 + 100);

	my $resurrect = $env->sync();
	is($resurrect->{summary}{pulled}, 1, 'master edit resurrects a client-deleted file');
	is($env->read_file('client', 'gone.txt'), "edited on the server\n", 'client got the new content');
	is($resurrect->{exit}, 3, 'the run reports a warning');
	like(join(' ', @{ $resurrect->{warnings} }), qr/deleted on test-1/, 'warning explains what happened');

	# Master deletes kept.txt while the client edits it: the edit wins.
	unlink $env->server_path('kept.txt');



( run in 1.579 second using v1.01-cache-2.11-cpan-eb134014566 )