package common

import (
	"testing"
)

func TestParseProjectMeta(t *testing.T) {
	res, err := parseProjectMeta([]byte(metaPrjData))

	if err != nil {
		t.Fatal(err)
	}

	if len(res.BuildFlags.Contents) < 1 {
		t.Fatalf("Build flags are not as expected")
	}
}

func TestParsingOfBuildResults(t *testing.T) {
	res, err := parseBuildResults([]byte(buildResultData))

	if err != nil {
		t.Fatal(err)
	}

	if res.Result[0].Project != "home:adamm" ||
		res.Result[0].Status[1].Details != "nothing provides libBox2D-devel" ||
		res.Result[0].Status[0].Code != "excluded" {

		t.Fatal(res.Result)
	}

	if res.Result[0].Binaries[0].Package != "Nudoku" ||
		len(res.Result[0].Binaries[0].Binary) != 0 {

		t.Fatal(res.Result[0].Binaries[0])
	}
}

const metaPrjData = `
<project name="home:adamm">
  <title>Adam's Home Projects</title>
  <description/>
  <person userid="adamm" role="maintainer"/>
  <build>
    <enable/>
  </build>
  <publish>
    <enable/>
  </publish>
  <repository name="openSUSE_Tumbleweed">
    <path project="openSUSE:Tumbleweed" repository="standard"/>
    <arch>x86_64</arch>
    <arch>aarch64</arch>
  </repository>
  <repository name="containerfile">
    <path project="openSUSE:Containers:Tumbleweed" repository="containers"/>
    <path project="openSUSE:Factory:ARM" repository="standard"/>
    <arch>aarch64</arch>
  </repository>
  <repository name="container">
    <path project="openSUSE:Containers:Tumbleweed" repository="containers"/>
    <path project="openSUSE:Factory:ARM" repository="standard"/>
    <arch>aarch64</arch>
  </repository>
</project>
`

const buildResultData = `
<resultlist state="4c058c53835d10cbbfc6f0ee546a921f">
  <result project="home:adamm" repository="openSUSE_Tumbleweed" arch="x86_64" code="published" state="published">
    <status package="Nudoku" code="excluded"/>
    <status package="geekofall" code="unresolvable">
      <details>nothing provides libBox2D-devel</details>
    </status>
    <status package="geekofall.1" code="unresolvable">
      <details>nothing provides libBox2D-devel</details>
    </status>
    <status package="http-parser" code="failed"/>
    <status package="minicoredumper" code="failed"/>
    <status package="opensuse-dovecot-image" code="broken">
      <details>conflict in file _service</details>
    </status>
    <status package="opensuse-nsd-image" code="disabled"/>
    <status package="opensuse-opendkim-image" code="excluded"/>
    <status package="test" code="broken">
      <details>no source uploaded</details>
    </status>
    <status package="visual-studio-rpi-pico-sshd-image" code="excluded"/>
    <binarylist package="Nudoku"/>
    <binarylist package="geekofall">
      <binary filename="_buildenv" size="162675" mtime="1613557547"/>
      <binary filename="_statistics" size="955" mtime="1613557547"/>
      <binary filename="geekofall-1536436213.a078a60-13.37.src.rpm" size="30213" mtime="1613557547"/>
      <binary filename="geekofall-1536436213.a078a60-13.37.x86_64.rpm" size="87666" mtime="1613557547"/>
      <binary filename="geekofall-debuginfo-1536436213.a078a60-13.37.x86_64.rpm" size="43740" mtime="1613557547"/>
      <binary filename="rpmlint.log" size="990" mtime="1613557546"/>
    </binarylist>
    <binarylist package="geekofall.1">
      <binary filename="_buildenv" size="161411" mtime="1613557546"/>
      <binary filename="_statistics" size="955" mtime="1613557546"/>
      <binary filename="geekofall-1.0-1.36.src.rpm" size="39526" mtime="1613557547"/>
      <binary filename="geekofall-1.0-1.36.x86_64.rpm" size="87583" mtime="1613557547"/>
      <binary filename="geekofall-debuginfo-1.0-1.36.x86_64.rpm" size="43907" mtime="1613557547"/>
      <binary filename="rpmlint.log" size="308" mtime="1613557546"/>
    </binarylist>
    <binarylist package="http-parser">
      <binary filename="_buildenv" size="13162" mtime="1490341842"/>
      <binary filename="_statistics" size="702" mtime="1490341842"/>
      <binary filename="http-parser-2.7.1-1.7.src.rpm" size="58213" mtime="1490341842"/>
      <binary filename="libhttp_parser-devel-2.7.1-1.7.x86_64.rpm" size="11596" mtime="1490341843"/>
      <binary filename="libhttp_parser2-2.7.1-1.7.x86_64.rpm" size="24523" mtime="1490341843"/>
      <binary filename="rpmlint.log" size="322" mtime="1490341842"/>
    </binarylist>
    <binarylist package="minicoredumper">
      <binary filename="_buildenv" size="160856" mtime="1631825754"/>
      <binary filename="_statistics" size="952" mtime="1631825754"/>
      <binary filename="libminicoredumper2-2.0.1-2.41.x86_64.rpm" size="11789" mtime="1631825754"/>
      <binary filename="libminicoredumper2-debuginfo-2.0.1-2.41.x86_64.rpm" size="17071" mtime="1631825755"/>
      <binary filename="minicoredumper-2.0.1-2.41.src.rpm" size="303867" mtime="1631825755"/>
      <binary filename="minicoredumper-2.0.1-2.41.x86_64.rpm" size="41509" mtime="1631825755"/>
      <binary filename="minicoredumper-debuginfo-2.0.1-2.41.x86_64.rpm" size="83137" mtime="1631825755"/>
      <binary filename="minicoredumper-debugsource-2.0.1-2.41.x86_64.rpm" size="40197" mtime="1631825755"/>
      <binary filename="minicoredumper-devel-2.0.1-2.41.x86_64.rpm" size="14717" mtime="1631825755"/>
      <binary filename="minicoredumper-utils-2.0.1-2.41.x86_64.rpm" size="21336" mtime="1631825755"/>
      <binary filename="minicoredumper-utils-debuginfo-2.0.1-2.41.x86_64.rpm" size="32338" mtime="1631825755"/>
      <binary filename="rpmlint.log" size="1466" mtime="1631825754"/>
    </binarylist>
    <binarylist package="opensuse-dovecot-image"/>
    <binarylist package="opensuse-nsd-image"/>
    <binarylist package="opensuse-opendkim-image"/>
    <binarylist package="test"/>
    <binarylist package="visual-studio-rpi-pico-sshd-image"/>
  </result>
  <result project="home:adamm" repository="openSUSE_Tumbleweed" arch="aarch64" code="published" state="published">
    <status package="Nudoku" code="excluded"/>
    <status package="geekofall" code="unresolvable">
      <details>nothing provides libBox2D-devel</details>
    </status>
    <status package="geekofall.1" code="unresolvable">
      <details>nothing provides libBox2D-devel</details>
    </status>
    <status package="http-parser" code="failed"/>
    <status package="minicoredumper" code="failed"/>
    <status package="opensuse-dovecot-image" code="broken">
      <details>conflict in file _service</details>
    </status>
    <status package="opensuse-nsd-image" code="disabled"/>
    <status package="opensuse-opendkim-image" code="excluded"/>
    <status package="test" code="broken">
      <details>no source uploaded</details>
    </status>
    <status package="visual-studio-rpi-pico-sshd-image" code="excluded"/>
    <binarylist package="Nudoku"/>
    <binarylist package="geekofall"/>
    <binarylist package="geekofall.1"/>
    <binarylist package="http-parser"/>
    <binarylist package="minicoredumper"/>
    <binarylist package="opensuse-dovecot-image"/>
    <binarylist package="opensuse-nsd-image"/>
    <binarylist package="opensuse-opendkim-image"/>
    <binarylist package="test"/>
    <binarylist package="visual-studio-rpi-pico-sshd-image"/>
  </result>
  <result project="home:adamm" repository="containerfile" arch="aarch64" code="published" state="published">
    <status package="Nudoku" code="excluded"/>
    <status package="geekofall" code="excluded"/>
    <status package="geekofall.1" code="excluded"/>
    <status package="http-parser" code="excluded"/>
    <status package="minicoredumper" code="excluded"/>
    <status package="opensuse-dovecot-image" code="broken">
      <details>conflict in file _service</details>
    </status>
    <status package="opensuse-nsd-image" code="disabled"/>
    <status package="opensuse-opendkim-image" code="excluded"/>
    <status package="test" code="broken">
      <details>no source uploaded</details>
    </status>
    <status package="visual-studio-rpi-pico-sshd-image" code="succeeded"/>
    <binarylist package="Nudoku"/>
    <binarylist package="geekofall"/>
    <binarylist package="geekofall.1"/>
    <binarylist package="http-parser"/>
    <binarylist package="minicoredumper"/>
    <binarylist package="opensuse-dovecot-image"/>
    <binarylist package="opensuse-nsd-image"/>
    <binarylist package="opensuse-opendkim-image"/>
    <binarylist package="test"/>
    <binarylist package="visual-studio-rpi-pico-sshd-image">
      <binary filename="_blob.sha256:9ea20f002e582c3fc8d14062450a6c4a4165317961d2af9b9e7b0d4e080f1c3c" size="38617846" mtime="1719856307"/>
      <binary filename="_blob.sha256:9f9058a297fba8e8d5234de28cee922e039e4f622722514d7012f36d5a4eabd9" size="312194914" mtime="1721404809"/>
      <binary filename="_blob.sha256:ac4e7722af8bc4c504a796b53ff48c5071b52de2da633845cd0c74cfd32dec9f" size="6845" mtime="1721404809"/>
      <binary filename="_buildenv" size="144706" mtime="1721404745"/>
      <binary filename="_statistics" size="1012" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.basepackages" size="19094" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.cdx.json" size="102955" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.containerinfo" size="1364" mtime="1721404809"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.obsbinlnk" size="701" mtime="1721404809"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.packages" size="30189" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.report" size="66947" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.spdx.json" size="13509128" mtime="1721404745"/>
      <binary filename="rpi-pico-vscode.aarch64-6.182.tar" size="350823936" mtime="1721404745"/>
    </binarylist>
  </result>
  <result project="home:adamm" repository="container" arch="aarch64" code="published" state="published">
    <status package="Nudoku" code="excluded"/>
    <status package="geekofall" code="excluded"/>
    <status package="geekofall.1" code="excluded"/>
    <status package="http-parser" code="excluded"/>
    <status package="minicoredumper" code="excluded"/>
    <status package="opensuse-dovecot-image" code="broken">
      <details>conflict in file _service</details>
    </status>
    <status package="opensuse-nsd-image" code="failed"/>
    <status package="opensuse-opendkim-image" code="unresolvable">
      <details>have choice for /usr/bin/openssl needed by opendkim: libressl openssl-1_0_0 openssl-3</details>
    </status>
    <status package="test" code="broken">
      <details>no source uploaded</details>
    </status>
    <status package="visual-studio-rpi-pico-sshd-image" code="excluded"/>
    <binarylist package="Nudoku"/>
    <binarylist package="geekofall"/>
    <binarylist package="geekofall.1"/>
    <binarylist package="http-parser"/>
    <binarylist package="minicoredumper"/>
    <binarylist package="opensuse-dovecot-image">
      <binary filename="_blob.sha256:6b4fea0b9f77d0a5b66de148fb28109c5012e0831d30ff14fefa953fd06b3bfb" size="4545634" mtime="1715975625"/>
      <binary filename="_blob.sha256:b1e156fe04a0d30d27c7989c6403346f343212f39cc3ffdb0ab31207b3a11734" size="2579" mtime="1716584488"/>
      <binary filename="_blob.sha256:c6588a65781845c90cda432251b7b098a64b94e98261a39306451c8318c9e130" size="31074584" mtime="1716584488"/>
      <binary filename="_buildenv" size="144566" mtime="1716584481"/>
      <binary filename="_statistics" size="1011" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.basepackages" size="9920" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.cdx.json" size="48775" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.containerinfo" size="1485" mtime="1716584488"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.obsbinlnk" size="745" mtime="1716584488"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.spdx.json" size="2008180" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.tar" size="35627520" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.tar.sha256" size="125" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.docker.tar.sha256.asc" size="481" mtime="1716584489"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.packages" size="15559" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.report" size="33131" mtime="1716584481"/>
      <binary filename="opensuse-dovecot-image.aarch64-1.0.0-Build15.21.verified" size="156" mtime="1716584481"/>
    </binarylist>
    <binarylist package="opensuse-nsd-image">
      <binary filename="_blob.sha256:109084b53bf1ad4a0d98db0cb442ecb197d73fe946351001a6b4b756369ce074" size="4545407" mtime="1712925763"/>
      <binary filename="_blob.sha256:8fdaf68f1e41f06656875fc0f29fc248e7011cef766fbdda2a2061a6b0722b9c" size="2531" mtime="1713297940"/>
      <binary filename="_blob.sha256:db672aec4faacd3c9cefb56bde6795ed638c5239cc1aa3160f87e0965b0f03cd" size="9675022" mtime="1713297940"/>
      <binary filename="_buildenv" size="142894" mtime="1713297937"/>
      <binary filename="_statistics" size="1009" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.basepackages" size="9920" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.cdx.json" size="41631" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.containerinfo" size="1453" mtime="1713297940"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.obsbinlnk" size="710" mtime="1713297940"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.spdx.json" size="549331" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.tar" size="14226944" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.tar.sha256" size="120" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.docker.tar.sha256.asc" size="481" mtime="1713297940"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.packages" size="13321" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.report" size="28055" mtime="1713297937"/>
      <binary filename="opensuse-nsd-image.aarch64-1.0.0-Build7.86.verified" size="156" mtime="1713297937"/>
    </binarylist>
    <binarylist package="opensuse-opendkim-image"/>
    <binarylist package="test"/>
    <binarylist package="visual-studio-rpi-pico-sshd-image"/>
  </result>
</resultlist>
`
