<commit_before>package digests

import (
	"fmt"
	"sort"
	"testing"

	"github.com/ion-channel/ionic/scanner"
	"github.com/ion-channel/ionic/scans"

	"github.com/franela/goblin"
	. "github.com/onsi/gomega"
)

func TestDigest(t *testing.T) {
	g := goblin.Goblin(t)
	RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

	g.Describe("Digest", func() {
		g.Describe("Sorting", func() {
			g.It("should be sortable by index", func() {
				ds := []Digest{
					Digest{Index: 1},
					Digest{Index: 3},
					Digest{Index: 2},
					Digest{Index: 0},
				}

				Expect(ds[0].Index).To(Equal(1))
				Expect(ds[1].Index).To(Equal(3))
				Expect(ds[2].Index).To(Equal(2))
				Expect(ds[3].Index).To(Equal(0))

				sort.Slice(ds, func(i, j int) bool { return ds[i].Index < ds[j].Index })

				Expect(ds[0].Index).To(Equal(0))
				Expect(ds[1].Index).To(Equal(1))
				Expect(ds[2].Index).To(Equal(2))
				Expect(ds[3].Index).To(Equal(3))
			})
		})

		g.Describe("Strings", func() {
			g.It("should return string in JSON", func() {
				d := Digest{
					Index:          12,
					Title:          "sometitle",
					Data:           nil,
					ScanID:         "somescanid",
					RuleID:         "someruleid",
					RulesetID:      "somerulesetid",
					Evaluated:      true,
					Pending:        false,
					Passed:         true,
					PassedMessage:  "somepassedmessage",
					Warning:        false,
					WarningMessage: "somewarningmessage",
					Errored:        false,
					ErroredMessage: "someerroredmessage",
					singularTitle:  "somesingulartitle",
					pluralTitle:    "somepluraltitle",
				}
				Expect(fmt.Sprintf("%v", d)).To(Equal(`{"index":12,"title":"sometitle","data":null,"source_data":null,"scan_id":"somescanid","rule_id":"someruleid","ruleset_id":"somerulesetid","evaluated":true,"pending":false,"passed":true,"passed_message":"somepassedmessage","warning":false,"warning_message":"somewarningmessage","errored":false,"errored_message":"someerroredmessage"}`))
			})
		})

		g.Describe("States", func() {
			g.It("should be marked as pending when no status is provided", func() {
				ds := NewDigest(nil, 0, "", "")
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeTrue())
			})

			g.It("should show an error if present", func() {
				s := &scanner.ScanStatus{
					Status:  scanner.AnalysisStatusErrored,
					Message: "failed to perform the scan for a reason",
				}
				ds := NewDigest(s, 0, "", "")
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())
				Expect(ds.Errored).To(BeTrue())
				Expect(ds.ErroredMessage).To(Equal("failed to perform the scan for a reason"))

				s = &scanner.ScanStatus{
					Status:  scanner.AnalysisStatusFinished,
					Message: "completed scan",
				}
				ds = NewDigest(s, 0, "", "")
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())
				Expect(ds.Errored).To(BeFalse())
				Expect(ds.ErroredMessage).To(Equal("evaluation not received"))
			})

			g.It("should show an error when an invalid count is appended", func() {
				s := &scanner.ScanStatus{
					Status:  "errored",
					Message: "failed to perform the scan for a reason",
				}
				ds := NewDigest(s, 0, "", "")
				e := scans.NewEval()

				ds.AppendEval(e, "count", -1)
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())<commit_msg>show test failing<commit_after>package digests

import (
	"encoding/json"
	"fmt"
	"sort"
	"testing"

	"github.com/ion-channel/ionic/scanner"
	"github.com/ion-channel/ionic/scans"

	"github.com/franela/goblin"
	. "github.com/onsi/gomega"
)

func TestDigest(t *testing.T) {
	g := goblin.Goblin(t)
	RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

	g.Describe("Digest", func() {
		g.Describe("Sorting", func() {
			g.It("should be sortable by index", func() {
				ds := []Digest{
					Digest{Index: 1},
					Digest{Index: 3},
					Digest{Index: 2},
					Digest{Index: 0},
				}

				Expect(ds[0].Index).To(Equal(1))
				Expect(ds[1].Index).To(Equal(3))
				Expect(ds[2].Index).To(Equal(2))
				Expect(ds[3].Index).To(Equal(0))

				sort.Slice(ds, func(i, j int) bool { return ds[i].Index < ds[j].Index })

				Expect(ds[0].Index).To(Equal(0))
				Expect(ds[1].Index).To(Equal(1))
				Expect(ds[2].Index).To(Equal(2))
				Expect(ds[3].Index).To(Equal(3))
			})
		})

		g.Describe("Strings", func() {
			g.It("should return string in JSON", func() {
				d := Digest{
					Index:          12,
					Title:          "sometitle",
					Data:           nil,
					ScanID:         "somescanid",
					RuleID:         "someruleid",
					RulesetID:      "somerulesetid",
					Evaluated:      true,
					Pending:        false,
					Passed:         true,
					PassedMessage:  "somepassedmessage",
					Warning:        false,
					WarningMessage: "somewarningmessage",
					Errored:        false,
					ErroredMessage: "someerroredmessage",
					singularTitle:  "somesingulartitle",
					pluralTitle:    "somepluraltitle",
				}
				Expect(fmt.Sprintf("%v", d)).To(Equal(`{"index":12,"title":"sometitle","data":null,"source_data":null,"scan_id":"somescanid","rule_id":"someruleid","ruleset_id":"somerulesetid","evaluated":true,"pending":false,"passed":true,"passed_message":"somepassedmessage","warning":false,"warning_message":"somewarningmessage","errored":false,"errored_message":"someerroredmessage"}`))
			})
		})

		g.Describe("States", func() {
			g.It("should be marked as pending when no status is provided", func() {
				ds := NewDigest(nil, 0, "", "")
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeTrue())
			})

			g.It("should show an error if present", func() {
				s := &scanner.ScanStatus{
					Status:  scanner.AnalysisStatusErrored,
					Message: "failed to perform the scan for a reason",
				}
				ds := NewDigest(s, 0, "", "")
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())
				Expect(ds.Errored).To(BeTrue())
				Expect(ds.ErroredMessage).To(Equal("failed to perform the scan for a reason"))

				s = &scanner.ScanStatus{
					Status:  scanner.AnalysisStatusFinished,
					Message: "completed scan",
				}
				ds = NewDigest(s, 0, "", "")
				j, _ := json.Marshal(ds)
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())
				Expect(ds.Errored).To(BeTrue())
				Expect(ds.ErroredMessage).To(Equal("evaluation not received"))
			})

			g.It("should show an error when an invalid count is appended", func() {
				s := &scanner.ScanStatus{
					Status:  "errored",
					Message: "failed to perform the scan for a reason",
				}
				ds := NewDigest(s, 0, "", "")
				e := scans.NewEval()

				ds.AppendEval(e, "count", -1)
				Expect(ds).NotTo(BeNil())
				Expect(ds.Pending).To(BeFalse())
{'fraction_non_alphanumeric': 0.18120198825124265, 'fraction_numerical': 0.008736255460159662, 'mean_word_length': 3.303305249513934, 'pattern_counts': {'":': 30, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3705003', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2339, 'n_words': 406}