<commit_before>package service

import (
	"testing"
	"time"

	"github.com/jonboulle/clockwork"
	"golang.org/x/net/context"

	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol"
	"github.com/keybase/gregor/protocol/gregor1"
)

func TestRekeyNeededMessageNoScores(t *testing.T) {
	tc := libkb.SetupTest(t, "gregor", 1)
	defer tc.Cleanup()

	tc.G.SetService()

	kbUID := keybase1.MakeTestUID(1)
	gUID := gregor1.UID(kbUID.ToBytes())
	did, err := libkb.NewDeviceID()
	if err != nil {
		t.Fatal(err)
	}
	tc.G.Env.GetConfigWriter().SetUserConfig(libkb.NewUserConfig(kbUID, "", nil, did), true)

	h, err := newGregorHandler(tc.G)
	if err != nil {
		t.Fatal(err)
	}

	rekeyHandler := NewRekeyUIHandler(tc.G, 0)
	rekeyHandler.alwaysAlive = true
	h.PushHandler(rekeyHandler)

	msgID := gregor1.MsgID("my_random_id")
	m := gregor1.Message{
		Ibm_: &gregor1.InBandMessage{
			StateUpdate_: &gregor1.StateUpdateMessage{
				Md_: gregor1.Metadata{
					MsgID_: msgID,
					Ctime_: gregor1.ToTime(tc.G.Clock.Now()),
					Uid_:   gUID,
				},
				Creation_: &gregor1.Item{
					Category_: gregor1.Category("kbfs_tlf_rekey_needed"),
					Body_:     gregor1.Body(`[]`),
				},
			},
		},
	}

	if err := h.BroadcastMessage(context.Background(), m); err != nil {
		t.Fatal(err)
	}
}

const problemSet = `{ 
	"user": {
		"uid": "295a7eea607af32040647123732bc819",
		"username": "t_alice"
	},
	"kid": "1212121212",
	"tlfs": [
		{
			"tlf": {
				"tlfid": "folder", 
				"name": "folder name", 
				"writers": ["t_alice"], 
				"readers": ["t_alice"], 
				"isPrivate": true
			},
			"score": 300,
			"solutions": []
		}
	]
}`

// XXX I don't know why this won't unmarshal into
// keybase1.ProblemSet:<commit_msg>Fix all tests after rebase<commit_after>package service

import (
	"testing"
	"time"

	"github.com/jonboulle/clockwork"
	"golang.org/x/net/context"

	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol"
	"github.com/keybase/gregor/protocol/gregor1"
)

func TestRekeyNeededMessageNoScores(t *testing.T) {
	tc := libkb.SetupTest(t, "gregor", 1)
	defer tc.Cleanup()

	tc.G.SetService()

	kbUID := keybase1.MakeTestUID(1)
	gUID := gregor1.UID(kbUID.ToBytes())
	did, err := libkb.NewDeviceID()
	if err != nil {
		t.Fatal(err)
	}
	tc.G.Env.GetConfigWriter().SetUserConfig(libkb.NewUserConfig(kbUID, "", nil, did), true)

	h, err := newGregorHandler(tc.G)
	if err != nil {
		t.Fatal(err)
	}

	rekeyHandler := NewRekeyUIHandler(tc.G, 0)
	rekeyHandler.alwaysAlive = true
	h.PushHandler(rekeyHandler)

	msgID := gregor1.MsgID("my_random_id")
	m := gregor1.Message{
		Ibm_: &gregor1.InBandMessage{
			StateUpdate_: &gregor1.StateUpdateMessage{
				Md_: gregor1.Metadata{
					MsgID_: msgID,
					Ctime_: gregor1.ToTime(tc.G.Clock.Now()),
					Uid_:   gUID,
				},
				Creation_: &gregor1.Item{
					Category_: gregor1.Category("kbfs_tlf_rekey_needed"),
					Body_:     gregor1.Body(`{}`),
				},
			},
		},
	}

	if err := h.BroadcastMessage(context.Background(), m); err != nil {
		t.Fatal(err)
	}
}

const problemSet = `{ 
	"user": {
		"uid": "295a7eea607af32040647123732bc819",
		"username": "t_alice"
	},
	"kid": "011212121212121212121212121212121212121212121212121212121212121212120a",
	"tlfs": [
		{
			"tlf": {
				"tlfid": "folder", 
				"name": "folder name", 
				"writers": ["t_alice"], 
				"readers": ["t_alice"], 
				"isPrivate": true
			},
			"score": 300,
			"solutions": ["011313131313131313131313131313131313131313131313131313131313131313130a"]
		}
	]
}`

// XXX I don't know why this won't unmarshal into
// keybase1.ProblemSet:
{'fraction_non_alphanumeric': 0.16801112656467315, 'fraction_numerical': 0.06564673157162726, 'mean_word_length': 3.8398384925975773, 'pattern_counts': {'":': 26, '<': 3, '<?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': '888320', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1517, 'n_words': 230}