Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/mygnoscan/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186404
  max_deposit = "14000000ugnot"

mygnoscan.gno

// Package mygnoscan builds links into a mygnoscan block explorer.
//
// A realm knows things a reader cannot see from its Render output: which chain
// it is on, what its address is, which block it was last written at. Turning
// any of those into a link means knowing the explorer's route table, and a
// route table hand-written into thirty realms is thirty places to fix when it
// moves. This package is the one place.
//
// # The routes are measured, not guessed
//
// Every path below was read out of the explorer's own router (its single-page
// frontend, `route()`), not inferred from the UI, on 2026-09-22. Two of them
// are retired and deliberately absent: `?tab=graph` (folded into `deps`, still
// redirected) and `?tab=inert` (folded into the default tab, silently dropped).
//
// # An unknown path renders the home page
//
// The explorer's router falls through to `home` rather than to a 404, so a
// misspelled route is not visible as an error: the link works, it just goes
// somewhere else. That is why the page names here are constants and why Page
// is the only door to them.
//
// # The network is per instance, not per chain
//
// The explorer takes `?network=<id>`, and the ids are chosen by whoever runs
// the instance: moul's names mainnet `mainnet`, while the upstream default
// config names the same chain `gnoland1`. NetworkFor maps a chain-id to the
// ids DefaultBase serves. Point Scanner at another instance and you very
// likely also want WithNetwork.
//
// Omitting the parameter is not the same as asking for mainnet: the explorer
// then blends every chain it indexes, which looks plausible and is wrong. A
// Scanner whose network is empty emits no parameter, so New on a chain nobody
// indexes produces a link to the blend. Set it explicitly when it matters.
//
// # Usage
//
// The whole point is a realm footer that stays right when the chain changes:
//
//	func Render(path string) string {
//		return body + "\n\n---\n\n" + mygnoscan.Default().CurrentRealmFooter()
//	}
//
// and, for a realm that wants to hand out sharper links:
//
//	s := mygnoscan.Default()
//	s.Address(someAddr)                              // an account
//	s.Block(runtime.ChainHeight())                   // the block we are at
//	s.Realm("gno.land/r/nym-moultest123/home", mygnoscan.TabSource)
//	s.RealmFunc("gno.land/r/nym-moultest123/config", "Set")     // straight at one function
//
// Nothing here reads chain state except ChainDomain, ChainID and ChainHeight,
// and nothing here writes. Building a link is free.
package mygnoscan

import (
	"chain/runtime"
	"chain/runtime/unsafe"
	"net/url"
	"strconv"
	"strings"
)

// DefaultBase is moul's instance, the one the rest of this repo links to.
const DefaultBase = "https://mygnoscan.moul.p2p.team"

// The realm-page tabs, as the explorer's own tabNames list has them. An
// unknown tab name lands on the default tab rather than erroring, so these
// exist to keep a typo out of a deployed realm.
const (
	TabInfo    = "info"    // the default; passing it emits no parameter
	TabDocs    = "docs"    // exported symbols
	TabSource  = "source"  // the .gno files as deployed
	TabCalls   = "calls"   // transactions that called this realm
	TabEvents  = "events"  // events it emitted
	TabStorage = "storage" // what it pays to store
	TabDefi    = "defi"    // balances and token positions
	TabDeps    = "deps"    // imports, dependents and the graph over them
)

// The explorer's list pages. Page takes one of these.
const (
	PageRealms     = "realms"
	PagePackages   = "packages"
	PageContracts  = "contracts"
	PageApps       = "apps"
	PageTxs        = "txs"
	PageBlocks     = "blocks"
	PageAccounts   = "accounts"
	PageDefi       = "defi"
	PageCoins      = "coins"
	PageGRC20      = "grc20"
	PageValidators = "validators"
	PageGovDAO     = "govdao"
	PageProposals  = "govdao/proposals"
	PageVoters     = "govdao/voters"
	PageOptions    = "govdao/options"
	PageParams     = "params"
	PageEvents     = "events"
	PageGas        = "gas"
	PageGasRealms  = "gas/realms"
	PageGasUsers   = "gas/users"
	PageGasTxs     = "gas/txs"
	PageStorage    = "storage"
	PageAnalytics  = "analytics"
	PageDashboards = "dashboards"
	PageSanity     = "sanity"
	PageWatch      = "watch"
)

// The sub-views of the packages and accounts pages, the only two list pages
// that restore a view from the URL.
//
// Every other list page writes its state into the query string (?by=, ?page=,
// ?status=, ?window=, ?section=, ?failed=, ?storage=, ?txs=) and does NOT read
// any of it back on load: a link carrying one of those opens the page on its
// default view instead, silently. So there are no helpers for them here.
// Measured against the explorer's router on 2026-09-22.
const (
	PackagesAll      = "all"      // every package
	PackagesInert    = "inert"    // the parked queue: submitted, not yet approved
	AccountsActivity = "activity" // ranked by what they have done
	AccountsBalances = "balances" // ranked by what they hold
)

// Scanner is one explorer instance plus the network its links should open on.
// It is a value: copying one is free and there is nothing to close.
type Scanner struct {
	base    string
	network string
}

// Default returns a Scanner pointed at DefaultBase, on whichever network
// answers for the running chain.
func Default() Scanner { return New(DefaultBase) }

// New returns a Scanner pointed at base, on whichever network answers for the
// running chain. A trailing slash on base is dropped so New("…/") and New("…")
// build the same links.
func New(base string) Scanner {
	return Scanner{
		base:    strings.TrimSuffix(base, "/"),
		network: NetworkFor(runtime.ChainID()),
	}
}

// WithNetwork returns a copy of s whose links carry ?network=id. The empty
// string removes the parameter, which asks the explorer for every chain at
// once rather than for this one.
func (s Scanner) WithNetwork(id string) Scanner {
	s.network = id
	return s
}

// Base returns the instance root, without a trailing slash.
func (s Scanner) Base() string { return s.base }

// Network returns the network id these links carry, empty when they carry none.
func (s Scanner) Network() string { return s.network }

// NetworkFor maps a chain-id to the network id DefaultBase serves it under, or
// "" when that instance does not index the chain (a gnodev, a local test).
//
// Verified against DefaultBase's /api/networks on 2026-09-22: it serves
// mainnet, pearl and staging. sapphire is mapped because the chain exists and
// an instance configured for it uses that id; DefaultBase currently answers
// for it with the blend, which is the same failure mode as an unknown chain
// and is why the caller can override with WithNetwork.
func NetworkFor(chainID string) string {
	switch chainID {
	case "gnoland-1":
		return "mainnet"
	case "pearl-1":
		return "pearl"
	case "sapphire-1":
		return "sapphire"
	case "staging":
		return "staging"
	default:
		return ""
	}
}

// URL is the one place a link is assembled: an absolute link to path on this
// instance, carrying query as key/value pairs plus the network. A pair with an
// empty key or value is dropped, so an optional parameter needs no branch at
// the call site.
//
// Prefer the typed helpers below; this is the escape hatch for a route added
// to the explorer after this package was last touched.
func (s Scanner) URL(path string, query ...string) string {
	if len(query)%2 != 0 {
		panic("mygnoscan: odd number of query arguments")
	}
	if path == "" {
		path = "/"
	} else if !strings.HasPrefix(path, "/") {
		path = "/" + path
	}

	v := url.Values{}
	for i := 0; i < len(query); i += 2 {
		if query[i] == "" || query[i+1] == "" {
			continue
		}
		v.Set(query[i], query[i+1])
	}
	if s.network != "" {
		v.Set("network", s.network)
	}

	out := s.base + path
	if len(v) > 0 {
		out += "?" + v.Encode()
	}
	return out
}

// Home links the explorer's front page on this network.
func (s Scanner) Home() string { return s.URL("/") }

// Page links one of the Page* list pages. A name that is not shaped like one
// falls back to the front page rather than to the explorer's own
// unknown-path-renders-home behaviour, which would look identical and mean
// something else.
func (s Scanner) Page(name string) string {
	n := strings.Trim(name, "/")
	if !safePath(n) {
		return s.Home()
	}
	return s.URL("/" + n)
}

// Packages links the package list, on the PackagesAll or PackagesInert view.
// The empty string takes the page's own default, which is PackagesAll.
func (s Scanner) Packages(view string) string {
	return s.URL("/"+PagePackages, "pv", view)
}

// Accounts links the account list, on the AccountsActivity or AccountsBalances
// view. The empty string takes the page's own default, which is
// AccountsActivity.
func (s Scanner) Accounts(view string) string {
	return s.URL("/"+PageAccounts, "av", view)
}

// Realm links a package or realm page, optionally opening one tab.
//
// pkgPath is accepted either fully qualified ("gno.land/r/nym-moultest123/home") or bare
// ("r/moul/home"): TrimDomain normalises it. Passing TabInfo, or no tab at
// all, emits no tab parameter.
func (s Scanner) Realm(pkgPath string, tab ...string) string {
	p := TrimDomain(pkgPath)
	if p == "" {
		return s.Home()
	}
	return s.URL("/realm/"+p, "tab", oneTab(tab))
}

// CurrentRealm links the page of the realm that called into this package.
//
// It stack-walks (unsafe.CurrentRealm), so it names whichever realm was
// current when it ran. For a plain read like this one that is the BORROWER,
// which is the wanted answer: a realm asking for "my page" gets its own, and
// so does one asking through a shared config realm, because a borrowed call
// opens no realm frame. Measured in the test harness on 2026-09-22.
//
// Inside a CROSSING function it names that function's realm instead. That is
// also correct and rarely what a link wants, so pass the path explicitly
// there. Never branch on it for authorisation either way.
func (s Scanner) CurrentRealm(tab ...string) string {
	return s.Realm(unsafe.CurrentRealm().PkgPath(), tab...)
}

// RealmFunc links a realm's source tab scrolled to one exported function.
func (s Scanner) RealmFunc(pkgPath, fn string) string {
	p := TrimDomain(pkgPath)
	if p == "" || fn == "" {
		return s.Realm(pkgPath)
	}
	return s.URL("/realm/"+p, "tab", TabSource, "fn", fn)
}

// RealmFile links a realm's source tab scrolled to one line of one file.
// A line of 0 or less links the file without an anchor.
func (s Scanner) RealmFile(pkgPath, file string, line int) string {
	p := TrimDomain(pkgPath)
	if p == "" || file == "" {
		return s.Realm(pkgPath)
	}
	if line <= 0 {
		return s.URL("/realm/"+p, "tab", TabSource, "file", file)
	}
	return s.URL("/realm/"+p, "tab", TabSource, "file", file, "line", strconv.Itoa(line))
}

// Address links an account page: its balance, its transactions, what it deployed.
//
// The address is escaped rather than trusted. `address` is a string type and
// nothing stops a realm casting a caller's input into one, so a value reaching
// here is not necessarily bech32.
func (s Scanner) Address(addr address) string {
	if addr == "" {
		return s.Page(PageAccounts)
	}
	return s.URL("/address/" + escapeSegment(addr.String()))
}

// Tx links one transaction. The hash is the base64 form the indexer and gnokey
// print, which carries "+", "/" and "="; the whole segment is percent-escaped
// so the explorer's decodeURIComponent gives it back byte for byte.
func (s Scanner) Tx(hash string) string {
	if hash == "" {
		return s.Page(PageTxs)
	}
	return s.URL("/tx/" + escapeSegment(hash))
}

// Block links one block by height.
func (s Scanner) Block(height int64) string {
	if height <= 0 {
		return s.Page(PageBlocks)
	}
	return s.URL("/block/" + strconv.FormatInt(height, 10))
}

// CurrentBlock links the block this call is executing in.
func (s Scanner) CurrentBlock() string { return s.Block(runtime.ChainHeight()) }

// Validator links one validator by address.
func (s Scanner) Validator(addr address) string {
	if addr == "" {
		return s.Page(PageValidators)
	}
	return s.URL("/validator/" + escapeSegment(addr.String()))
}

// Proposal links one GovDAO proposal by id.
func (s Scanner) Proposal(id int) string {
	if id < 0 {
		return s.Page(PageProposals)
	}
	return s.URL("/govdao/" + strconv.Itoa(id))
}

// Token links one GRC20 asset by its ledger key, which looks like
// "gno.land/r/gnoswap/gns.GNS.0000000". That key carries both slashes and
// dots, and neither is a route separator, so it is percent-escaped whole.
func (s Scanner) Token(key string) string {
	if key == "" {
		return s.Page(PageGRC20)
	}
	return s.URL("/grc20/" + escapeSegment(key))
}

// TrimDomain strips the chain domain from a package path, so both
// "gno.land/r/nym-moultest123/home" and "r/moul/home" become "r/moul/home". A path that
// is not shaped like a package path (see safePath) comes back empty, and every
// caller here treats empty as "no such target" and falls back to a list page.
//
// It tries the running chain's domain first, then the literal "gno.land/",
// because the explorer's own realm route reassembles the path by prepending
// "gno.land/" unconditionally: on a chain whose domain is something else, a
// bare path is still what the link must carry.
func TrimDomain(pkgPath string) string {
	p := strings.TrimPrefix(pkgPath, runtime.ChainDomain()+"/")
	p = strings.TrimPrefix(p, "gno.land/")
	p = strings.Trim(p, "/")
	if !safePath(p) {
		return ""
	}
	return p
}

// escapeSegment percent-encodes everything outside the RFC 3986 unreserved
// set, which is exactly what JavaScript's encodeURIComponent does and
// therefore exactly what the explorer's decodeURIComponent round-trips.
//
// url.PathEscape is the obvious choice and is wrong here twice: it leaves '/'
// alone in some modes and, more importantly, it never escapes '(' or ')',
// which are legal URL sub-delims and which close a markdown link early. Since
// every link this package builds may end up inside one, the segment escaper
// has to be stricter than the URL spec requires.
func escapeSegment(s string) string {
	const hex = "0123456789ABCDEF"
	var b strings.Builder
	for i := 0; i < len(s); i++ {
		c := s[i]
		switch {
		case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9',
			c == '-', c == '_', c == '.', c == '~':
			b.WriteByte(c)
		default:
			b.WriteByte('%')
			b.WriteByte(hex[c>>4])
			b.WriteByte(hex[c&0x0f])
		}
	}
	return b.String()
}

// safePath reports whether p is shaped like a package path: [A-Za-z0-9._/-],
// and no empty segment.
//
// This is a rendering guard, not a validity check. Everything built here ends
// up inside a markdown link, so a path carrying ')' would close the link early
// and the rest would render as page text; a path carrying a space or a newline
// breaks it differently. Percent-escaping cannot be the answer because '(' and
// ')' are legal sub-delims in a URL path and url.PathEscape leaves them alone,
// and escaping the separators would break the route. So the characters are
// refused instead.
//
// It matters because a realm may well pass a path a caller typed (a registry,
// a directory, a "link to my realm" form), and this package cannot tell.
func safePath(p string) bool {
	if p == "" {
		return false
	}
	lastSlash := true // a leading slash was already trimmed, so no empty first segment
	for i := 0; i < len(p); i++ {
		c := p[i]
		switch {
		case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
			lastSlash = false
		case c == '.', c == '-', c == '_':
			lastSlash = false
		case c == '/':
			if lastSlash {
				return false // "" or "a//b"
			}
			lastSlash = true
		default:
			return false
		}
	}
	return !lastSlash // no trailing slash left over
}

// Link renders a markdown link. Neither argument is escaped: everything this
// package builds is a path or a constant, never text a caller typed. Passing a
// user-supplied label through here would let it break out of the link.
func Link(text, target string) string { return "[" + text + "](" + target + ")" }

// RealmFooter is the line a realm appends under its Render output: the four
// views of itself worth one click, separated by middots.
//
// Deliberately plain markdown with no leading rule, so the caller decides
// where it sits and what separates it from the body.
func (s Scanner) RealmFooter(pkgPath string) string {
	p := TrimDomain(pkgPath)
	if p == "" {
		return ""
	}
	return Link("explorer", s.Realm(p)) +
		" · " + Link("source", s.Realm(p, TabSource)) +
		" · " + Link("calls", s.Realm(p, TabCalls)) +
		" · " + Link("deps", s.Realm(p, TabDeps))
}

// CurrentRealmFooter is RealmFooter for the realm that called in. It carries
// the same stack-walking caveat as CurrentRealm.
func (s Scanner) CurrentRealmFooter() string {
	return s.RealmFooter(unsafe.CurrentRealm().PkgPath())
}

// oneTab reads the optional tab argument. More than one is a call-site bug and
// panics rather than silently using the first, and TabInfo is dropped because
// it is the default the explorer lands on anyway.
func oneTab(tab []string) string {
	switch len(tab) {
	case 0:
		return ""
	case 1:
		if tab[0] == TabInfo {
			return ""
		}
		return tab[0]
	default:
		panic("mygnoscan: at most one tab")
	}
}

mygnoscan_test.gno

package mygnoscan

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// mainnet is what a Scanner looks like on the chain that matters. Tests build
// it explicitly rather than calling Default(), because the test harness runs
// with chain-id "dev", which NetworkFor maps to no network at all: pinning
// Default()'s output would pin the absence of the parameter and prove nothing
// about the case every real link is in.
func mainnet() Scanner { return New(DefaultBase).WithNetwork("mainnet") }

func TestNetworkFor(t *testing.T) {
	tests := []struct {
		chainID string
		want    string
	}{
		{"gnoland-1", "mainnet"},
		{"pearl-1", "pearl"},
		{"sapphire-1", "sapphire"},
		{"staging", "staging"},
		{"dev", ""},          // gnodev, and what `gno test` reports
		{"", ""},             // no chain at all
		{"gnoland1", ""},     // the upstream default instance's id, not a chain-id
		{"test3", ""},        // a retired testnet
		{"mainnet", ""},      // the network id, fed back in by mistake
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, NetworkFor(tt.chainID), "NetworkFor("+tt.chainID+")")
	}
}

func TestTrimDomain(t *testing.T) {
	tests := []struct {
		in   string
		want string
	}{
		{"gno.land/r/nym-moultest123/home", "r/moul/home"},
		{"r/moul/home", "r/moul/home"},
		{"/r/nym-moultest123/home", "r/moul/home"},
		{"gno.land/p/nym-moultest123/mygnoscan/v0", "p/moul/mygnoscan/v0"},
		{"gno.land/r/gnoland/blog", "r/gnoland/blog"},
		{"", ""},
		{"gno.land/", ""},
		// A path whose own name starts with the domain is only trimmed once,
		// so a realm cannot be linked out of its own namespace by its name.
		{"gno.land/r/nym-moultest123/gno.land", "r/moul/gno.land"},
		// Anything not shaped like a package path comes back empty, so the
		// caller falls back to a list page instead of emitting it.
		{"r/moul/home)", ""},
		{"r/moul/ho me", ""},
		{"r/moul//home", ""},
		{"r/moul/home\n", ""},
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, TrimDomain(tt.in), "TrimDomain("+tt.in+")")
	}
}

func TestBaseNormalisation(t *testing.T) {
	uassert.Equal(t, DefaultBase, New(DefaultBase+"/").Base())
	uassert.Equal(t, DefaultBase, New(DefaultBase).Base())
	uassert.Equal(t, "https://scan.example.com", New("https://scan.example.com/").Base())
}

func TestTargets(t *testing.T) {
	s := mainnet()
	const b = DefaultBase
	tests := []struct {
		name string
		got  string
		want string
	}{
		{"home", s.Home(), b + "/?network=mainnet"},
		{"page", s.Page(PageTxs), b + "/txs?network=mainnet"},
		{"page nested", s.Page(PageGasRealms), b + "/gas/realms?network=mainnet"},
		{"page slashed", s.Page("/blocks/"), b + "/blocks?network=mainnet"},
		{"realm qualified", s.Realm("gno.land/r/nym-moultest123/home"), b + "/realm/r/nym-moultest123/home?network=mainnet"},
		{"realm bare", s.Realm("r/moul/home"), b + "/realm/r/nym-moultest123/home?network=mainnet"},
		{"realm tab", s.Realm("r/moul/home", TabSource), b + "/realm/r/nym-moultest123/home?network=mainnet&tab=source"},
		// TabInfo is the tab the explorer lands on, so asking for it adds nothing.
		{"realm tab info", s.Realm("r/moul/home", TabInfo), b + "/realm/r/nym-moultest123/home?network=mainnet"},
		{"realm empty", s.Realm(""), b + "/?network=mainnet"},
		{"realm func", s.RealmFunc("r/moul/config", "Set"), b + "/realm/r/nym-moultest123/config?fn=Set&network=mainnet&tab=source"},
		{"realm file", s.RealmFile("r/moul/home", "render.gno", 42), b + "/realm/r/nym-moultest123/home?file=render.gno&line=42&network=mainnet&tab=source"},
		{"realm file no line", s.RealmFile("r/moul/home", "render.gno", 0), b + "/realm/r/nym-moultest123/home?file=render.gno&network=mainnet&tab=source"},
		{"address", s.Address("g1manfred47kzduec920z88wfr64ylksmdcedlf5"), b + "/address/g1manfred47kzduec920z88wfr64ylksmdcedlf5?network=mainnet"},
		{"address empty", s.Address(""), b + "/accounts?network=mainnet"},
		{"block", s.Block(80618), b + "/block/80618?network=mainnet"},
		{"block zero", s.Block(0), b + "/blocks?network=mainnet"},
		{"validator", s.Validator("g1manfred47kzduec920z88wfr64ylksmdcedlf5"), b + "/validator/g1manfred47kzduec920z88wfr64ylksmdcedlf5?network=mainnet"},
		{"proposal", s.Proposal(7), b + "/govdao/7?network=mainnet"},
		{"proposal negative", s.Proposal(-1), b + "/govdao/proposals?network=mainnet"},
		{"token empty", s.Token(""), b + "/grc20?network=mainnet"},
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, tt.got, tt.name)
	}
}

// TestEscaping covers the two routes whose argument is not URL-safe. Both are
// read back with decodeURIComponent by the explorer, so percent-escaping the
// whole segment is what round-trips; leaving the slashes alone would split the
// segment and land on a different route entirely.
func TestEscaping(t *testing.T) {
	s := mainnet()

	// A 32-byte hash in the base64 form the indexer and gnokey print, picked
	// to carry all three of the characters that make base64 awkward in a URL.
	// Everything outside the unreserved set moves, which is what the
	// explorer's decodeURIComponent undoes byte for byte.
	uassert.Equal(t,
		DefaultBase+"/tx/BppSxd7qRiAPsMHzDx5mys%2BYpeDCMUzxI1qeb%2FEhaYc%3D?network=mainnet",
		s.Tx("BppSxd7qRiAPsMHzDx5mys+YpeDCMUzxI1qeb/EhaYc="))
	uassert.Equal(t, DefaultBase+"/txs?network=mainnet", s.Tx(""))

	// A GRC20 ledger key carries both slashes and dots.
	uassert.Equal(t,
		DefaultBase+"/grc20/gno.land%2Fr%2Fgnoswap%2Fgns.GNS.0000000?network=mainnet",
		s.Token("gno.land/r/gnoswap/gns.GNS.0000000"))
}

func TestURLQuery(cur realm, t *testing.T) {
	s := mainnet()

	uassert.Equal(t, DefaultBase+"/anything?network=mainnet", s.URL("anything"))
	uassert.Equal(t, DefaultBase+"/anything?network=mainnet", s.URL("/anything"))
	uassert.Equal(t, DefaultBase+"/a?k=v&network=mainnet", s.URL("/a", "k", "v"))

	// An empty key or value is dropped, so an optional parameter needs no
	// branch at the call site.
	uassert.Equal(t, DefaultBase+"/a?network=mainnet", s.URL("/a", "k", ""))
	uassert.Equal(t, DefaultBase+"/a?network=mainnet", s.URL("/a", "", "v"))

	uassert.PanicsWithMessage(t, cur, "mygnoscan: odd number of query arguments", func() {
		s.URL("/a", "k")
	})
}

// TestNoNetwork pins what a Scanner emits when it does not know the chain:
// nothing. The explorer then answers for every chain it indexes at once, which
// is a real answer to a different question, so this is worth being explicit
// about rather than discovering from a plausible-looking page.
func TestNoNetwork(t *testing.T) {
	s := New(DefaultBase).WithNetwork("")
	uassert.Equal(t, "", s.Network())
	uassert.Equal(t, DefaultBase+"/realm/r/nym-moultest123/home", s.Realm("r/moul/home"))
	uassert.Equal(t, DefaultBase+"/realm/r/nym-moultest123/home?tab=calls", s.Realm("r/moul/home", TabCalls))
	uassert.Equal(t, DefaultBase+"/", s.Home())
}

// TestDefaultUnderTest pins Default() as the test harness sees it: chain-id
// "dev" is indexed nowhere, so the links carry no network. A realm rendering
// under gnodev gets exactly this, which is why a local preview link is not
// proof that the mainnet one is right.
func TestDefaultUnderTest(t *testing.T) {
	s := Default()
	uassert.Equal(t, DefaultBase, s.Base())
	uassert.Equal(t, "", s.Network())
	uassert.Equal(t, DefaultBase+"/realm/r/nym-moultest123/home", s.Realm("gno.land/r/nym-moultest123/home"))
}

func TestTabArity(cur realm, t *testing.T) {
	s := mainnet()
	uassert.PanicsWithMessage(t, cur, "mygnoscan: at most one tab", func() {
		s.Realm("r/moul/home", TabSource, TabCalls)
	})
}

func TestLink(t *testing.T) {
	uassert.Equal(t, "[text](https://example.com)", Link("text", "https://example.com"))
}

func TestRealmFooter(t *testing.T) {
	s := mainnet()
	const want = "[explorer](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/home?network=mainnet)" +
		" · [source](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/home?network=mainnet&tab=source)" +
		" · [calls](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/home?network=mainnet&tab=calls)" +
		" · [deps](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/home?network=mainnet&tab=deps)"
	uassert.Equal(t, want, s.RealmFooter("gno.land/r/nym-moultest123/home"))
	uassert.Equal(t, want, s.RealmFooter("r/moul/home"))
	uassert.Equal(t, "", s.RealmFooter(""))
}

// TestOtherInstance is the whole reason base and network are separate knobs:
// a second instance serving the same chain under a different id.
func TestOtherInstance(t *testing.T) {
	s := New("https://scan.example.com").WithNetwork("gnoland1")
	uassert.Equal(t,
		"https://scan.example.com/realm/r/nym-moultest123/home?network=gnoland1",
		s.Realm("gno.land/r/nym-moultest123/home"))
}

// TestMarkdownBreakout is the reason safePath and escapeSegment exist. A realm
// that links a path or a hash a caller typed would otherwise let that caller
// close the markdown link early and write page text of their choosing.
//
// url.PathEscape is not enough on its own: "(" and ")" are legal URL
// sub-delims and it leaves both alone.
func TestMarkdownBreakout(t *testing.T) {
	s := mainnet()

	// A path is refused outright, because escaping its separators would break
	// the route. The caller gets the front page, not a broken link.
	uassert.Equal(t, DefaultBase+"/?network=mainnet",
		s.Realm("r/evil/x)](https://evil.example.com"))
	uassert.Equal(t, "", s.RealmFooter("r/evil/x)](https://evil.example.com"))

	// A hash and a token key are opaque, so they are escaped rather than
	// refused, and the parentheses move.
	uassert.Equal(t,
		DefaultBase+"/tx/x%29%5D%28evil?network=mainnet",
		s.Tx("x)](evil"))
	uassert.Equal(t,
		DefaultBase+"/grc20/x%29%5D%28evil?network=mainnet",
		s.Token("x)](evil"))

	// An address is a string type; nothing stops a realm casting into it.
	uassert.Equal(t,
		DefaultBase+"/address/g1%29%5D%28evil?network=mainnet",
		s.Address(address("g1)](evil")))

	// A page name that is not a page name lands on the front page, rather
	// than on the explorer's own unknown-path-renders-home behaviour, which
	// would look identical and mean something else.
	uassert.Equal(t, DefaultBase+"/?network=mainnet", s.Page("txs)](evil"))
	uassert.Equal(t, DefaultBase+"/?network=mainnet", s.Page(""))
}

func TestSafePath(t *testing.T) {
	tests := []struct {
		in   string
		want bool
	}{
		{"r/moul/home", true},
		{"p/moul/mygnoscan/v0", true},
		{"gas/realms", true},
		{"a", true},
		{"a.b-c_d", true},
		{"", false},
		{"a//b", false},
		{"a/", false},
		{"a b", false},
		{"a)b", false},
		{"a(b", false},
		{"a]b", false},
		{"a\nb", false},
		{"a?b", false},
		{"a#b", false},
		{"a%b", false},
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, safePath(tt.in), "safePath("+tt.in+")")
	}
}

func TestSubViews(t *testing.T) {
	s := mainnet()
	uassert.Equal(t, DefaultBase+"/packages?network=mainnet&pv=inert", s.Packages(PackagesInert))
	uassert.Equal(t, DefaultBase+"/packages?network=mainnet", s.Packages(""))
	uassert.Equal(t, DefaultBase+"/accounts?av=balances&network=mainnet", s.Accounts(AccountsBalances))
	uassert.Equal(t, DefaultBase+"/accounts?network=mainnet", s.Accounts(""))
}