<commit_before>package cobe

import (
	"os"
	"runtime"
	"strings"
	"sync"
	"testing"
)

func TestShortLearn(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	b.Learn("cobe cobe cobe")

	r := b.Reply("cobe")
	if strings.Index(r, "cobe") != -1 {
		t.Fatalf("incorrectly learned cobe: %s", r)
	}
}

func TestReply(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	r := b.Reply("this this this is a test test test")
	if r == "" {
		t.Fatal("got a nil reply")
	}

	r = b.ReplyWithOptions("this this this is a test test test", ReplyOptions{})
	if r == "" {
		t.Fatal("got a nil reply with empty options")
	}
}

func TestAllowReply(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	shortOpts := ReplyOptions{DefaultReplyOptions.Duration, func(reply *Reply) bool {
		return len(reply.ToString()) < 140
	}}

	longOpts := ReplyOptions{DefaultReplyOptions.Duration, func(reply *Reply) bool {
		return len(reply.ToString()) > 140
	}}

	if len(b.ReplyWithOptions("test input", shortOpts)) >= 140 {
		t.Fatal("shortOpts reply is too long")
	}

	if len(b.ReplyWithOptions("test input", longOpts)) <= 140 {
		t.Fatal("longOpts reply is too short")
	}
}

// Run looped learn/reply on a brain to try to reproduce sqlite3 errors.
func TestLoop(t *testing.T) {
	// Test with an unreasonable number of GOMAXPROCS. This is a
	// temporary measure while ensuring single threaded acccess to
	// sqlite3.<commit_msg>Change the AllowReply test input to a known token<commit_after>package cobe

import (
	"os"
	"runtime"
	"strings"
	"sync"
	"testing"
)

func TestShortLearn(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	b.Learn("cobe cobe cobe")

	r := b.Reply("cobe")
	if strings.Index(r, "cobe") != -1 {
		t.Fatalf("incorrectly learned cobe: %s", r)
	}
}

func TestReply(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	r := b.Reply("this this this is a test test test")
	if r == "" {
		t.Fatal("got a nil reply")
	}

	r = b.ReplyWithOptions("this this this is a test test test", ReplyOptions{})
	if r == "" {
		t.Fatal("got a nil reply with empty options")
	}
}

func TestAllowReply(t *testing.T) {
	filename, err := tmpCopy("data/pg11.brain")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.Remove(filename)

	b, err := OpenCobe2Brain(filename)
	if err != nil {
		t.Fatal(err)
	}

	shortOpts := ReplyOptions{DefaultReplyOptions.Duration, func(reply *Reply) bool {
		return len(reply.ToString()) < 140
	}}

	longOpts := ReplyOptions{DefaultReplyOptions.Duration, func(reply *Reply) bool {
		return len(reply.ToString()) > 140
	}}

	if len(b.ReplyWithOptions("alice", shortOpts)) >= 140 {
		t.Fatal("shortOpts reply is too long")
	}

	if len(b.ReplyWithOptions("alice", longOpts)) <= 140 {
		t.Fatal("longOpts reply is too short")
	}
}

// Run looped learn/reply on a brain to try to reproduce sqlite3 errors.
func TestLoop(t *testing.T) {
	// Test with an unreasonable number of GOMAXPROCS. This is a
	// temporary measure while ensuring single threaded acccess to
	// sqlite3.
{'fraction_non_alphanumeric': 0.14957264957264957, 'fraction_numerical': 0.01282051282051282, 'mean_word_length': 3.7225725094577555, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4281333', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1392, 'n_words': 377}