Files
tailscale/feature/favorites/favorites_test.go
Will HannahandWill Hannah e1ffeeffba client,feature/favorites: add locally-pinned favorites (#20563)
Let clients pin favorite devices, exit nodes, and services so GUIs can
surface & change them. Pins are stored per login profile in the new
favorites feature module, keyed per category; devices and exit nodes by
StableNodeID, services by ServiceName.

The item types live in a leaf package feature/favorites/pintype,
keeping them out of the core ipn hierarchy. Each category has
its own type (pintype.Device, pintype.ExitNode, pintype.Service).

Exposed over LocalAPI at GET/POST /localapi/v0/pins, where POST replaces
only the categories named in the request so a client can update one
category without clobbering the others. Pins are local to the device and
are not synced across a user's devices.

updates tailscale/corp#44836

Signed-off-by: Will Hannah <[email protected]>
Co-authored-by: Will Hannah <[email protected]>
2026-08-19 09:52:56 -04:00

68 lines
1.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package favorites
import (
"testing"
"time"
"tailscale.com/feature/favorites/pintype"
"tailscale.com/ipn"
"tailscale.com/tsd"
"tailscale.com/tstest"
"tailscale.com/tstime"
"tailscale.com/types/logger"
)
// fakeBackend is a minimal [ipnext.SafeBackend] for tests. Only Clock and TailscaleVarRoot are
// used by the extension; Sys is never called.
type fakeBackend struct {
clock tstime.Clock
varRoot string
}
func (f *fakeBackend) Sys() *tsd.System { return nil }
func (f *fakeBackend) Clock() tstime.Clock { return f.clock }
func (f *fakeBackend) TailscaleVarRoot() string { return f.varRoot }
// newTestExtension returns an extension backed by a temp var root and a fixed test clock, with its
// current profile set to pid.
func newTestExtension(t *testing.T, pid ipn.ProfileID) *extension {
t.Helper()
e := &extension{
logf: logger.Discard,
sb: &fakeBackend{
clock: tstest.NewClock(tstest.ClockOpts{Start: time.Now()}),
varRoot: t.TempDir(),
},
}
e.onChangeProfile((&ipn.LoginProfile{ID: pid}).View(), ipn.PrefsView{}, false)
return e
}
func TestNoVarRootUsesMemory(t *testing.T) {
e := &extension{
logf: logger.Discard,
sb: &fakeBackend{
clock: tstest.NewClock(tstest.ClockOpts{Start: time.Now()}),
varRoot: "", // force the in-memory fallback
},
}
e.onChangeProfile((&ipn.LoginProfile{ID: "pid"}).View(), ipn.PrefsView{}, false)
if _, err := e.setPins(pintype.SetRequest{
Pins: pintype.Set{Devices: []pintype.Device{{ID: "n1"}}},
DevicesSet: true,
}); err != nil {
t.Fatal(err)
}
got, err := e.currentPins()
if err != nil {
t.Fatal(err)
}
if len(got.Devices) != 1 || got.Devices[0].ID != "n1" {
t.Errorf("in-memory store: got %v, want devices=[n1]", got.Devices)
}
}