#!/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? parser behavior regression tests

use strict;
use warnings;
use Test::More;
use lib './unix';

require './unix/sysinfo_client';

my $report = {};
my $result = tabDelimitedToHash($report, "system\thostname\thost.example\n");
is($report->{'system'}{'hostname'}, 'host.example', 'simple key parsed');
is($result->{'rejected'}, 0, 'no rejected lines for valid input');

$report = {};
$result = tabDelimitedToHash($report, "system\t0\tvalue-zero-key\n");
is($report->{'system'}{'0'}, 'value-zero-key', 'key 0 is accepted');

$report = {
   'system' => {
      'hostname' => 'old',
   },
};
$result = tabDelimitedToHash($report, "system\thostname\tnew\n");
is($report->{'system'}{'hostname'}, 'new', 'later scalar replaces earlier scalar');

$report = {
   'system' => 'scalar',
};
$result = tabDelimitedToHash($report, "system\thostname\tnew\n");
is($result->{'rejected'}, 1, 'descending through scalar is rejected');
like($result->{'firstRejected'}, qr/^system\thostname\tnew$/, 'first rejected line captured');

$report = {
   'system' => {
      'hostname' => 'value',
   },
};
$result = tabDelimitedToHash($report, "system\thostname\textra\tnew\n");
is($result->{'rejected'}, 1, 'descending through scalar to map is rejected');

$report = {};
$result = tabDelimitedToHash($report, "system\t\tvalue\n");
is($result->{'rejected'}, 1, 'empty key segment rejected');

$report = {};
$result = tabDelimitedToHash($report, "garbage-line-without-tabs\n");
is($result->{'rejected'}, 1, 'single-field garbage rejected');

$report = {};
my $rawValue = 'A"BC\\D';
$result = tabDelimitedToHash($report, join("\t", 'system', 'quoted', $rawValue) . "\n");
is($report->{'system'}{'quoted'}, $rawValue, 'raw value preserved without escaping changes');

done_testing();
