<commit_before>package api

import (
	"fmt"
	"github.com/martinp/atbapi/atb"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestMarshal(t *testing.T) {
	data := struct{ Foo string }{"bar"}
	b, err := marshal(data, true)
	if err != nil {
		t.Fatal(err)
	}
	actual := string(b)
	expected := "{\n  \"Foo\": \"bar\"\n}"
	if actual != expected {
		t.Fatalf("Expected '%s', got '%s'", expected, actual)
	}
	b, err = marshal(data, false)
	if err != nil {
		t.Fatal(err)
	}
	actual = string(b)
	expected = "{\"Foo\":\"bar\"}"
	if actual != expected {
		t.Fatalf("Expected '%s', got '%s'", expected, actual)
	}
}

func newTestServer(path string, body string) *httptest.Server {
	handler := func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8")
		fmt.Fprint(w, body)
	}
	mux := http.NewServeMux()
	mux.HandleFunc(path, handler)
	return httptest.NewServer(mux)
}

func TestGetBusStops(t *testing.T) {
	server := newTestServer("/", busStopsResponse)
	defer server.Close()
	atb := atb.Client{URL: server.URL}
	api := New(atb)
	_, err := api.getBusStops()
	if err != nil {
		t.Fatal(err)
	}
	cached, ok := api.cache.Get("stops")
	if !ok {
		t.Fatal("Expected true")
	}
	busStops, ok := cached.(BusStops)
	if !ok {
		t.Fatal("Expected true")
	}
	if len(busStops.Stops) != 1 {
		t.Fatal("Expected length to be 1")
	}
	if len(busStops.nodeIds) != 1 {
		t.Fatal("Expected length to be 1")
	}
}

func TestGetDepartures(t *testing.T) {
	server := newTestServer("/", forecastResponse)
	defer server.Close()
	atb := atb.Client{URL: server.URL}
	api := New(atb)
	_, err := api.getDepartures(16011376)
	if err != nil {
		t.Fatal(err)
	}
	cached, ok := api.cache.Get("16011376")
	if !ok {
		t.Fatal("Expected true")
	}
	departures, ok := cached.(Departures)
	if !ok {
		t.Fatal("Expected true")
	}
	if len(departures.Departures) != 1 {
		t.Fatal("Expected length to be 1")
	}
}

const busStopsResponse = `<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body><commit_msg>Fix tests<commit_after>package api

import (
	"fmt"
	"github.com/martinp/atbapi/atb"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func TestMarshal(t *testing.T) {
	data := struct{ Foo string }{"bar"}
	b, err := marshal(data, true)
	if err != nil {
		t.Fatal(err)
	}
	actual := string(b)
	expected := "{\n  \"Foo\": \"bar\"\n}"
	if actual != expected {
		t.Fatalf("Expected '%s', got '%s'", expected, actual)
	}
	b, err = marshal(data, false)
	if err != nil {
		t.Fatal(err)
	}
	actual = string(b)
	expected = "{\"Foo\":\"bar\"}"
	if actual != expected {
		t.Fatalf("Expected '%s', got '%s'", expected, actual)
	}
}

func newTestServer(path string, body string) *httptest.Server {
	handler := func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8")
		fmt.Fprint(w, body)
	}
	mux := http.NewServeMux()
	mux.HandleFunc(path, handler)
	return httptest.NewServer(mux)
}

func TestGetBusStops(t *testing.T) {
	server := newTestServer("/", busStopsResponse)
	defer server.Close()
	atb := atb.Client{URL: server.URL}
	api := New(atb, 168*time.Hour, 1*time.Minute)
	_, err := api.getBusStops()
	if err != nil {
		t.Fatal(err)
	}
	cached, ok := api.cache.Get("stops")
	if !ok {
		t.Fatal("Expected true")
	}
	busStops, ok := cached.(BusStops)
	if !ok {
		t.Fatal("Expected true")
	}
	if len(busStops.Stops) != 1 {
		t.Fatal("Expected length to be 1")
	}
	if len(busStops.nodeIds) != 1 {
		t.Fatal("Expected length to be 1")
	}
}

func TestGetDepartures(t *testing.T) {
	server := newTestServer("/", forecastResponse)
	defer server.Close()
	atb := atb.Client{URL: server.URL}
	api := New(atb, 168*time.Hour, 1*time.Minute)
	_, err := api.getDepartures(16011376)
	if err != nil {
		t.Fatal(err)
	}
	cached, ok := api.cache.Get("16011376")
	if !ok {
		t.Fatal("Expected true")
	}
	departures, ok := cached.(Departures)
	if !ok {
		t.Fatal("Expected true")
	}
	if len(departures.Departures) != 1 {
		t.Fatal("Expected length to be 1")
	}
}

const busStopsResponse = `<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
{'fraction_non_alphanumeric': 0.1927927927927928, 'fraction_numerical': 0.023873873873873873, 'mean_word_length': 4.035147392290249, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 2, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 10}, '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': '7437265', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1807, 'n_words': 369}