<commit_before>// A set of tests for AGI in Go
//
// Copyright (C) 2013 - 2014, <NAME> <<EMAIL>>
// This program is free software, distributed under the terms of
// the BSD 3-Clause License. See the LICENSE file
// at the top of the source tree.
//
// Based on agi-test.agi from asterisk source tree.
// Can be used both as standalone AGI app or a FastAGI server
// if called with the flag '-spawn_fagi'

package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"net"

	"github.com/zaf/agi"
)

var listen = flag.Bool("spawn_fagi", false, "Spawn as a FastAGI server")

func main() {
	flag.Parse()
	if *listen {
		//If called as a FastAGI server
		ln, err := net.Listen("tcp", ":4573")
		if err != nil {
			log.Fatal(err)
		}
		defer ln.Close()
		for {
			conn, err := ln.Accept()
			if err != nil {
				log.Println(err)
				continue
			}
			go spawnAgi(conn)
		}
	} else {
		//If called as standalone AGI app
		spawnAgi(nil)
	}
}

func spawnAgi(c net.Conn) {
	var myAgi *agi.Session
	var err error
	if c != nil {
		//Create a new FastAGI session
		rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c))
		myAgi, err = agi.Init(rw)
		defer func() {
			c.Close()
			myAgi.Destroy()
		}()
	} else {
		//Create a new AGI session
		myAgi, err = agi.Init(nil)
		defer myAgi.Destroy()
	}
	if err != nil {
		log.Printf("Error Parsing AGI environment: %v\n", err)
		return
	}
	testAgi(myAgi)
	return
}

func testAgi(sess *agi.Session) {
	//Perform some tests
	var tests, pass, fail int
	var err error
	var r agi.Reply

	sess.Verbose("Testing answer...")
	r, err = sess.Answer()
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing channelstatus...")
	r, err = sess.ChannelStatus()
	if err != nil || r.Res != 6 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databaseput...")
	r, err = sess.DatabasePut("test", "my_key", "true")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databaseget...")
	r, err = sess.DatabaseGet("test", "my_key")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databasedel...")
	r, err = sess.DatabaseDel("test", "my_key")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databasedeltree...")
	r, err = sess.DatabaseDelTree("test")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing streamfile...")
	r, err = sess.StreamFile("beep", "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing sendtext...")
	r, err = sess.SendText("Hello World")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing sendimage...")
	r, err = sess.SendImage("asterisk-image")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing saynumber...")
	r, err = sess.SayNumber(192837465, "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing wait for digit...")
	r, err = sess.WaitForDigit(3000)
	if err != nil || r.Res == -1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing redord...")
	r, err = sess.RecordFile("/tmp/testagi", "alaw", "1234567890*#", 3000)
	if err != nil || r.Res == -1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing record playback...")
	r, err = sess.StreamFile("/tmp/testagi", "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++<commit_msg>Two small fixes in agi-test example: A typo and a test case.<commit_after>// A set of tests for AGI in Go
//
// Copyright (C) 2013 - 2014, <NAME> <<EMAIL>>
// This program is free software, distributed under the terms of
// the BSD 3-Clause License. See the LICENSE file
// at the top of the source tree.
//
// Based on agi-test.agi from asterisk source tree.
// Can be used both as standalone AGI app or a FastAGI server
// if called with the flag '-spawn_fagi'

package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"net"

	"github.com/zaf/agi"
)

var listen = flag.Bool("spawn_fagi", false, "Spawn as a FastAGI server")

func main() {
	flag.Parse()
	if *listen {
		//If called as a FastAGI server
		ln, err := net.Listen("tcp", ":4573")
		if err != nil {
			log.Fatal(err)
		}
		defer ln.Close()
		for {
			conn, err := ln.Accept()
			if err != nil {
				log.Println(err)
				continue
			}
			go spawnAgi(conn)
		}
	} else {
		//If called as standalone AGI app
		spawnAgi(nil)
	}
}

func spawnAgi(c net.Conn) {
	var myAgi *agi.Session
	var err error
	if c != nil {
		//Create a new FastAGI session
		rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c))
		myAgi, err = agi.Init(rw)
		defer func() {
			c.Close()
			myAgi.Destroy()
		}()
	} else {
		//Create a new AGI session
		myAgi, err = agi.Init(nil)
		defer myAgi.Destroy()
	}
	if err != nil {
		log.Printf("Error Parsing AGI environment: %v\n", err)
		return
	}
	testAgi(myAgi)
	return
}

func testAgi(sess *agi.Session) {
	//Perform some tests
	var tests, pass, fail int
	var err error
	var r agi.Reply

	sess.Verbose("Testing answer...")
	r, err = sess.Answer()
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing channelstatus...")
	r, err = sess.ChannelStatus()
	if err != nil || r.Res != 6 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databaseput...")
	r, err = sess.DatabasePut("test", "my_key", "true")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databaseget...")
	r, err = sess.DatabaseGet("test", "my_key")
	if err != nil || r.Res != 1 || r.Dat != "true" {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databasedel...")
	r, err = sess.DatabaseDel("test", "my_key")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing databasedeltree...")
	r, err = sess.DatabaseDelTree("test")
	if err != nil || r.Res != 1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing streamfile...")
	r, err = sess.StreamFile("beep", "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing sendtext...")
	r, err = sess.SendText("Hello World")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing sendimage...")
	r, err = sess.SendImage("asterisk-image")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing saynumber...")
	r, err = sess.SayNumber(192837465, "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing wait for digit...")
	r, err = sess.WaitForDigit(3000)
	if err != nil || r.Res == -1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing record...")
	r, err = sess.RecordFile("/tmp/testagi", "alaw", "1234567890*#", 3000)
	if err != nil || r.Res == -1 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
	}
	tests++

	sess.Verbose("Testing record playback...")
	r, err = sess.StreamFile("/tmp/testagi", "")
	if err != nil || r.Res != 0 {
		sess.Verbose("Failed.")
		fail++
	} else {
		pass++
{'fraction_non_alphanumeric': 0.18796605811262534, 'fraction_numerical': 0.013628182051941374, 'mean_word_length': 2.9228441754916794, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, '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': '727237', 'n_tokens_mistral': 3447, 'n_tokens_neox': 3143, 'n_words': 802}