#!/usr/bin/bash

. data/bubu.lib

echo "# Testing log parser..."

for logfile in tests/sample.Xorg.*.log; do
    lines=$(python3 xdiagnose/xorglog.py ${logfile})
    if [ $? -ne 0 ]; then
	fail "${logfile}: error code non-zero"
    else
	pass "${logfile}: error code 0"
    fi

    num_lines=$(echo "$lines" | wc -l)
    if [ $num_lines -lt 10 ]; then
	fail "${logfile}: too few lines returned"
    else
	pass "${logfile}: log content returned"
    fi

    xserver_version=$(echo "$lines" | grep 'Version :')
    if [ -z "$xserver_version" ]; then
	fail "${logfile}: no xserver version indicated"
    else
	pass "${logfile}: xserver version returned"
    fi

    boot_time=$(echo "$lines" | grep 'Boot Time : ' | cut -d: -f2-)
    real_boot_time=$(grep "Time:" ${logfile} | cut -d: -f3-)
    if [ -z "$boot_time" ]; then
	fail "${logfile}: no boot time indicated"
    elif [ -z "$real_boot_time" ]; then
	fail "${logfile}: couldn't determine boot time from sample.Xorg.0.log"
    elif [ "$boot_time" != "$real_boot_time" ]; then
	fail "${logfile}: incorrect boot time '$boot_time'; '$real_boot_time' expected"
    else
	pass "${logfile}: parsed correct boot time"
    fi
done

final_summary

exit 0
