#!/usr/bin/env perl

# Copyright (c) 2025, R. W. Rodolico
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Revision History:
# 2026-10-11 Copilot v1.0.0 r? initial config and server.info tests
# 2026-10-12 Copilot v1.0.1 r? verify Name key does not map to hostname

use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use File::Path qw(make_path);

use lib './unix';
use sysinfoconf qw(loadConfigurationFile readServerInfo getLastConfigurationPath getLastServerInfoPath);

my $tempDir = tempdir(CLEANUP => 1);
my $legacyDir = "$tempDir/etc/camp/sysinfo-client";
make_path($legacyDir);

my $legacyConfig = "$legacyDir/sysinfo-client.yaml";
open my $cfg, '>', $legacyConfig or die $!;
print {$cfg} "---\nclientName: Legacy Client\nhostname: old-host\n";
close($cfg);

my $loaded = loadConfigurationFile('sysinfo-client.yaml', [$legacyDir]);
ok(ref($loaded) eq 'ARRAY', 'legacy dash-name config was loaded');
is(getLastConfigurationPath(), $legacyConfig, 'last config path tracks selected file');

my $serverInfo = "$tempDir/server.info";
open my $si, '>', $serverInfo or die $!;
print {$si} "# comment\n";
print {$si} "Owner: New Client\n";
print {$si} "Hostname: host.example.com\n";
print {$si} "serial: SERIAL-123\n";
print {$si} "uuid: aa:bb:cc:dd\n";
print {$si} "location: Site A\n";
print {$si} "os_type: freebsd\n";
print {$si} "tags: one, two, , three\n";
print {$si} "name: ignored-because-hostname-already-set\n";
print {$si} "unknown: keep-out\n";
print {$si} "serial: \n";
close($si);

my $parsed = readServerInfo($serverInfo);
is($parsed->{'clientName'}, 'New Client', 'owner maps to clientName');
is($parsed->{'hostname'}, 'host.example.com', 'hostname maps correctly');
is($parsed->{'serialNumber'}, 'SERIAL-123', 'serial maps correctly and empty later value does not override');
is($parsed->{'UUID'}, 'aa:bb:cc:dd', 'uuid maps correctly with colon content intact');
is($parsed->{'location'}, 'Site A', 'location maps correctly');
is($parsed->{'os_type'}, 'freebsd', 'os_type maps correctly');
is_deeply($parsed->{'tags'}, ['one', 'two', 'three'], 'tags split and trimmed');
ok(!exists($parsed->{'unknown'}), 'unknown key ignored');
is(getLastServerInfoPath(), $serverInfo, 'last server.info path tracks selected file');

my $nameOnlyServerInfo = "$tempDir/server-name-only.info";
open my $sino, '>', $nameOnlyServerInfo or die $!;
print {$sino} "Name: foo\n";
close($sino);

my $nameOnlyParsed = readServerInfo($nameOnlyServerInfo);
ok(!exists($nameOnlyParsed->{'hostname'}), 'Name key does not map to hostname when Hostname key is absent');

done_testing();
