#!/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-12 Copilot v1.0.0 r? tests for dotted-key path handling in compare_reports

use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use YAML::Tiny;

#######################################################
#
# runCompare( $oldPath, $newPath )
#
# Runs compare_reports and returns output plus exit code.
#
# Parameters:
#   $oldPath - path to old YAML report
#   $newPath - path to new YAML report
#
# Returns:
#   list (output, exit code)
#
# Side Effects:
#   executes external perl script
#
#######################################################
sub runCompare {
   my ($oldPath, $newPath) = @_;
   my $output = qx($^X tools/compare_reports "$oldPath" "$newPath" 2>&1);
   my $exitCode = $? >> 8;
   return ($output, $exitCode);
}

my $tempDir = tempdir(CLEANUP => 1);

my $oldAllowed = "$tempDir/old-allowed.yaml";
my $newAllowed = "$tempDir/new-allowed.yaml";
YAML::Tiny->new({
   'diskinfo' => {
      'eth0.100' => {
         'size' => '100',
      },
   },
})->write($oldAllowed);
YAML::Tiny->new({
   'diskinfo' => {
      'eth0.100' => {
         'size' => '200',
      },
   },
})->write($newAllowed);

my ($allowedOut, $allowedExit) = runCompare($oldAllowed, $newAllowed);
is($allowedExit, 0, 'dotted key under allowed path does not fail compare_reports');
like($allowedOut, qr/\(none\)/, 'allowed dotted-key diff appears as no disallowed value differences');

my $oldCollision = "$tempDir/old-collision.yaml";
my $newCollision = "$tempDir/new-collision.yaml";
YAML::Tiny->new({
   'a' => {
      'b.c' => 'first',
   },
   'a.b' => {
      'c' => 'second',
   },
})->write($oldCollision);
YAML::Tiny->new({
   'a' => {
      'b.c' => 'first',
   },
})->write($newCollision);

my ($collisionOut, $collisionExit) = runCompare($oldCollision, $newCollision);
is($collisionExit, 1, 'segment-safe flattening detects missing colliding dotted path');
like($collisionOut, qr/Missing old key paths in NEW:/, 'missing section is reported for colliding dotted keys');

done_testing();
