<commit_before>package main

import (
	"html/template"
	"net/http/httptest"
	"os"
	"path"
	"path/filepath"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestSimpleConfig(t *testing.T) {

	defaultConfig := `
{
	"Port": "8080",
	"Hostname": "localhost",
	"IndexSection": [
		{
			"Handlers": [
				{
					"Path": "/var/www/wiki/",
					"Prefix": "/",
					"Default": "index",
					"ServerType": "markdown",
					"Restricted": []
				}
			]
		}
	]
}
`

	filepath := path.Join(os.TempDir(), "simpleini.txt")
	f, err := os.Create(filepath)
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(filepath)
	if _, err := f.WriteString(defaultConfig); err != nil {
		t.Fatal(err)
	}
	if err := f.Close(); err != nil {
		t.Fatal(err)
	}

	success := LoadConfig(filepath)

	assert.NoError(t, success, "Default configuration should load without error.")

	config := GetConfig()

	//assert.Nil(t, config, "Config file could not be accessed")

	assert.Equal(t, config.Port, "8080", "read Port value incorrectly")
	assert.Equal(t, config.Hostname, "localhost", "read Hostname value incorrectly")

	assert.Equal(t, config.Indexes[0].Handlers[0].Path, "/var/www/wiki/", "read Path value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].Prefix, "/", "read Prefix value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].Default, "index", "read Default page value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].ServerType, "markdown", "read ServerType value incorrectly")

	assert.Equal(t, len(config.Indexes[0].Handlers[0].Restricted), 0, "incorrect number of restricted elements") // putting this comment here so sublime stops freaking out about a line with one character
}

func TestGetConfig(t *testing.T) {
	// directly put a config in the spot
	staticConfig = &GlobalSection{
		Port:        "8080",
		Hostname:    "localhost",
		TemplateDir: "/templates/",
		Redirects: []RedirectSection{
			RedirectSection{
				Requested: "source",
				Target:    "dest",
				Code:      302,
			},
		},
		Indexes: []IndexSection{
			IndexSection{
				WatchDirs: map[string]string{
					"/var/www/": "/",
				},
				WatchExtension: ".md",
				IndexPath:      "/index/",
				IndexType:      "en",<commit_msg>buggered the panic fro the missing indexsection down to a testing error<commit_after>package main

import (
	"html/template"
	"net/http/httptest"
	"os"
	"path"
	"path/filepath"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestSimpleConfig(t *testing.T) {

	defaultConfig := `
{
	"Port": "8080",
	"Hostname": "localhost",
	"IndexSection": [
		{
			"Handlers": [
				{
					"Path": "/var/www/wiki/",
					"Prefix": "/",
					"Default": "index",
					"ServerType": "markdown",
					"Restricted": []
				}
			]
		}
	]
}
`

	filepath := path.Join(os.TempDir(), "simpleini.txt")
	f, err := os.Create(filepath)
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(filepath)
	if _, err := f.WriteString(defaultConfig); err != nil {
		t.Fatal(err)
	}
	if err := f.Close(); err != nil {
		t.Fatal(err)
	}

	success := LoadConfig(filepath)

	assert.NoError(t, success, "Default configuration should load without error.")

	config := GetConfig()

	//assert.Nil(t, config, "Config file could not be accessed")

	assert.Equal(t, config.Port, "8080", "read Port value incorrectly")
	assert.Equal(t, config.Hostname, "localhost", "read Hostname value incorrectly")

	if len(config.Indexes) < 1 {
		t.Fatal("did not have enough indexes")
	}
	if len(config.Indexes[0].Handlers) < 1 {
		t.Fatal("did not have enough handlers")
	}

	assert.Equal(t, config.Indexes[0].Handlers[0].Path, "/var/www/wiki/", "read Path value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].Prefix, "/", "read Prefix value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].Default, "index", "read Default page value incorrectly")
	assert.Equal(t, config.Indexes[0].Handlers[0].ServerType, "markdown", "read ServerType value incorrectly")

	assert.Equal(t, len(config.Indexes[0].Handlers[0].Restricted), 0, "incorrect number of restricted elements") // putting this comment here so sublime stops freaking out about a line with one character
}

func TestGetConfig(t *testing.T) {
	// directly put a config in the spot
	staticConfig = &GlobalSection{
		Port:        "8080",
		Hostname:    "localhost",
		TemplateDir: "/templates/",
		Redirects: []RedirectSection{
			RedirectSection{
				Requested: "source",
				Target:    "dest",
				Code:      302,
			},
		},
		Indexes: []IndexSection{
			IndexSection{
				WatchDirs: map[string]string{
					"/var/www/": "/",
				},
				WatchExtension: ".md",
				IndexPath:      "/index/",
				IndexType:      "en",
{'fraction_non_alphanumeric': 0.16481876332622603, 'fraction_numerical': 0.011727078891257996, 'mean_word_length': 3.7004008016032066, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '862179', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1606, 'n_words': 380}