aboutsummaryrefslogtreecommitdiff
path: root/internal/game/correct_vanished_account_reset_test.go
blob: d40f602b99b04d6ac3e96d6f61ce88da9933c5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package game

// Regression: a session whose account file has vanished (purged, or deleted
// through the admin raw file editor) must be reset to the account-name prompt
// with no pending registration state left behind. Clearing Account alone left
// PendingPass set, so an already-buffered confirm-password line could still
// reach a handler that dereferences the account; applyNewAccount now also
// refuses to dereference a nil account.

import (
	"strings"
	"testing"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func TestPublishAccountUpdateVanishedAccountResetsSession(t *testing.T) {
	h := newHarness(t)

	conn := newChanConn()
	sess := &net.Session{Conn: conn, Account: &player.Account{Name: "bob"}}
	sess.SetState(net.StateNewAccount)
	sess.PendingPass = "pw"
	h.hub.Add(sess)

	h.g.RunOnOwner(func() { h.g.publishAccountUpdate("bob", "bob") })

	if got := sess.State(); got != net.StateAccountName {
		t.Errorf("state = %v, want StateAccountName", got)
	}
	if sess.Account != nil {
		t.Errorf("account not cleared: %+v", sess.Account)
	}
	if sess.PendingPass != "" {
		t.Errorf("PendingPass = %q, want it cleared with the account", sess.PendingPass)
	}
	out := string(conn.Bytes())
	if !strings.Contains(out, "Your account no longer exists.") {
		t.Errorf("missing vanished-account notice; output = %q", out)
	}
	if !strings.Contains(out, "Account name: ") {
		t.Errorf("missing account-name prompt; output = %q", out)
	}
}

func TestApplyNewAccountResetsWhenAccountVanished(t *testing.T) {
	h := newHarness(t)

	conn := newChanConn()
	sess := &net.Session{Conn: conn}
	sess.SetState(net.StateNewAccount)
	sess.PendingPass = "pw"

	// Direct call: the menu handlers run on the owner goroutine, so the test
	// goroutine stands in for it and an unguarded dereference fails loudly.
	h.g.applyNewAccount(sess, "hash", nil)

	if got := sess.State(); got != net.StateAccountName {
		t.Errorf("state = %v, want StateAccountName", got)
	}
	if sess.Account != nil {
		t.Errorf("account not cleared: %+v", sess.Account)
	}
	if sess.PendingPass != "" {
		t.Errorf("PendingPass = %q, want it cleared", sess.PendingPass)
	}
	out := string(conn.Bytes())
	if !strings.Contains(out, "Account name: ") {
		t.Errorf("missing account-name prompt; output = %q", out)
	}
	if _, err := h.g.AccountStore.LoadAccount("bob"); err == nil {
		t.Error("an account was created for a session with no account bound")
	}
}