#!/usr/bin/env bash
TEST_CATEGORY=core

level=$1
if [ -z "$level" ]; then
  level=1
else
  shift
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$level>"

source this
source test.suite

log.level $level

# Verify $HOME/.ssh/config (the current user's config) contains the canonical
# Host aliases — WODA.test, WODA.dev.root, WODA.dev, github.com — along with
# their key fields. Order between blocks is irrelevant; lines from different
# blocks may interleave. Each required line is verified independently with
# `grep -Fq`.

cfg="$HOME/.ssh/config"

test.case $level "ssh.config.woda: $USER has ~/.ssh/config" test -f "$cfg"
if [ ! -f "$cfg" ]; then
  expect.fail "$cfg missing"
  test.suite.save.results
  return 0 2>/dev/null || exit 0
else
  expect.pass "$cfg present"
fi

# Required fixed-string lines (Host headers + non-path fields). IdentityFile
# is checked separately by ssh.config.woda.portable + ssh.config.invariant.
# The presence of the four Host headers + their User/HostName here is
# sufficient to detect the regression we care about (configs wiped by
# user.init).
required_lines=(
  # WODA.test
  "WODA.test::Host WODA.test"
  "WODA.test::User root"
  "WODA.test::HostName 178.254.18.182"
  # WODA.dev.root
  "WODA.dev.root::Host WODA.dev.root"
  "WODA.dev.root::HostName cerulean.it"
  # (User root already required above)
  # WODA.dev
  "WODA.dev::Host WODA.dev"
  "WODA.dev::User developking"
  # (HostName cerulean.it already required above)
  # github.com (replaces the legacy 2cuGitHub alias)
  "github.com::Host github.com"
  "github.com::User git"
  "github.com::HostName github.com"
)

missing=()
for req in "${required_lines[@]}"; do
  label="${req%%::*}"
  line="${req#*::}"
  if ! grep -Fq -- "$line" "$cfg" 2>/dev/null; then
    missing+=("[$label] $line")
  fi
done

test.case $level "ssh.config.woda: $USER contains all WODA+github.com fields" true
if [ ${#missing[@]} -eq 0 ]; then
  expect.pass "$USER has all required Host aliases and fields"
else
  expect.fail "$USER $cfg is missing: $(printf '%s; ' "${missing[@]}")"
fi

test.suite.save.results
