#!/usr/bin/perl -w

use strict;
use Test;

BEGIN { plan tests => 16 }

use URI::Bookmarks;

my $sample_file = 't/sample.bookmarks';

open(COLL, $sample_file) or die "Couldn't open $sample_file: $!\n";
my $HTML = join '', <COLL>;
close(COLL);

print "Testing new(file => ...) constructor ...\n";
my $collection = new URI::Bookmarks(file => $sample_file);
die "$collection\n" unless ref $collection;
ok($collection ? 1 : 0);

print "Testing title() ...\n";
ok($collection->title, 'Bookmarks for Adam Spiers (title)');


print "Testing origin() ...\n";
my $origin = $collection->origin();
ok($origin->{type}, 'Netscape bookmarks file');
ok($origin->{doctype}, '<!DOCTYPE NETSCAPE-Bookmark-file-1>');
ok($origin->{preamble}, qr/This is an auto.*Edit!/s);
ok($collection->origin->{source}, $sample_file);

print "Testing export() ...\n";
my @lines = $collection->export('Netscape array');
ok(lc join('', @lines), lc $HTML);
open(OUT, ">foo");
print OUT lc join('', @lines);

print "Testing name_to_nodes() and build_name_lookup() ...\n";
my @search = $collection->name_to_nodes('Search');
ok(scalar(@search), 1);
ok(exists $search[0]->attribute->{folded});
my $autos = ($collection->name_to_nodes('Autos'))[0];
ok($autos->attribute->{href},
   'http://home.netscape.com/bookmark/4_7/ptchannelautos.html');
ok($autos->attribute->{add_date}, '940802141');
$autos->name('Search');
$collection->build_name_lookup();
@search = $collection->name_to_nodes('Search');
ok(scalar(@search), 2);

print "Testing all_URLs ...\n";
my @all_URLs = $collection->all_URLs;
ok(scalar(@all_URLs), 112);

print "Testing tree_root() and object classes ...\n";
ok(ref $collection->tree_root(), 'URI::Bookmark::Folder');
$autos = ($collection->name_to_nodes('People'))[0];
ok(ref $autos, 'URI::Bookmark::Entry');
my $channels = ($collection->name_to_nodes('Channels'))[0];
ok(ref $channels, 'URI::Bookmark::Folder');
