planetest

package
v1.801.490 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package planetest is the money peer a billing test bills against: commerce's half of the internal plane, on a real socket, recording every debit that crosses.

THE DEBIT LEFT HTTP. It used to be a JSON POST to commerce's /v1/billing/usage, and metering.Usage.Ref — the ledger's idempotency key — is tagged `json:"-"` so that no request body anywhere can set it. json.Marshal therefore dropped it and every split-deploy debit reached the ledger anonymous, which broke the exactly-once contract the same client holds on its co-resident path. The debit now crosses the plane, where the act's name is a field of its own.

The fakes in the app packages still answer the balance READ over HTTP, because that is where it still happens. They receive the usage DEBIT here.

IT IS ONE PACKAGE RATHER THAN A COPY PER CALLER, for the reason iamtest states and this file then proved: nine packages hand-rolled a usage counter against the HTTP body, the debit moved to the plane, and all nine kept counting an endpoint nothing calls any more. Every one of them reported zero debits and every one of them was asserting its own fixture. One recorder means the next time the wire moves, it moves in one place and the tests that depend on it fail loudly instead of quietly passing.

OBSERVING HERE GRANTS NOTHING. The socket is a per-test temp dir the test itself owns; no deployment binds or reads it.

Index

Constants

View Source
const Workdir = "/mnt/data"

Workdir is where a fake sandbox keeps its files — the same directory the real `exec` class uses, because the code tool tells the model to persist artifacts there and a test against a different one would prove nothing about the contract.

Variables

This section is empty.

Functions

func Cents

func Cents(m plane.Money) int64

Cents reads a crossed amount as whole cents — the unit a fee constant is written in. Use Micros for a per-screen or per-token price, which is finer than a cent and would truncate to zero here.

func Micros

func Micros(m plane.Money) int64

Micros reads a crossed amount as micro-USD (1e6 = $1) — finer than a cent, because per-token charges are.

The amount crosses as an EXACT decimal, so this is a rescale and never a rounding: the old HTTP body had to choose between a cents field and a micros field, and the reader had to guess which one was set.

func Wait

func Wait(cond func() bool) bool

Wait polls cond briefly and reports whether it came true. A debit is written on a detached goroutine, so an assertion made the instant the request returns is a race the test loses about one run in four.

Types

type Commerce

type Commerce struct {
	// contains filtered or unexported fields
}

Commerce records every debit that reaches the money plane.

func Serve

func Serve(t *testing.T) *Commerce

Serve binds the peer's socket and returns the recorder. Every test that expects a debit calls it, and it must be called BEFORE the client makes one — an unbound socket is plane.ErrNoPeer, which reads in a failure message as "the meter never fired" rather than "nobody was listening".

func ServeWith

func ServeWith(t *testing.T, observe func(org string, in plane.RecordIn)) *Commerce

ServeWith is Serve plus an observer, for a peer that keeps a running balance rather than only a count. The observer runs INSIDE the handler, so the debit has landed by the time the op answers — which is what lets a gate that reads afterwards see it.

func (*Commerce) All

func (c *Commerce) All() []Debit

All is every debit that crossed, oldest first.

func (*Commerce) Body

func (c *Commerce) Body() []byte

Body is the last debit rendered in the shape commerce's ledger row takes: the same field names the HTTP body used, so an assertion written against the old wire keeps asserting the same FACTS about the same debit.

It is a projection for reading, never a thing the code under test produces — the crossing is typed. Amount is in CENTS, which is what the callers compare against their fee constants; anything finer is in Micros.

func (*Commerce) Count

func (c *Commerce) Count() int32

Count is how many debits have landed. Debits are fire-and-forget, so callers poll it — see Wait.

func (*Commerce) Last

func (c *Commerce) Last() (Debit, bool)

Last is the most recent debit, and whether there was one.

func (*Commerce) Org

func (c *Commerce) Org() string

Org is the tenant the last debit was billed to, or "" if none landed. It is the CALLER's org, so a wrong value here is a cross-tenant leak rather than a typo.

func (*Commerce) Subject

func (c *Commerce) Subject() string

Subject is the wallet the last debit named, or "" if none landed.

type Debit

type Debit struct {
	Org string
	In  plane.RecordIn
}

Debit is one crossing: the org the CALLER acted for, and what it sent.

type Pod

type Pod struct {
	Files map[string][]byte
	// contains filtered or unexported fields
}

Pod is one fake sandbox: its files, and the two clocks the shell commands a caller actually sends depend on — a file's mtime, and the marker a run stamps for itself.

type Program

type Program func(id string, argv []string) (stdout, stderr string, exit int, wrote map[string][]byte)

Program is what a run DOES: what it printed, whether it failed, and the files it left behind (keyed by path relative to Workdir). Nil means a run that printed nothing and wrote nothing.

type Sandboxes

type Sandboxes struct {

	// Run is the program every leased sandbox executes. Assign it before the call
	// under test; it is read under the peer's own lock.
	Run Program

	// OnLease observes the ORG each lease was taken for — the caller's plane
	// identity, which is the only thing a cross-tenant test can assert on. A
	// sandbox id says nothing about whose it is; this says exactly that.
	OnLease func(org string)
	// contains filtered or unexported fields
}

Sandboxes is the peer. Set Run to give the fake pods a program; leave it nil and every run is a silent success.

func ServeSandboxes

func ServeSandboxes(t *testing.T) *Sandboxes

ServeSandboxes binds the sandboxes peer's socket and returns it. It shares the test's ONE runtime directory (see runtimeDir), so a test may serve this and the commerce peer together.

func (*Sandboxes) Args

func (s *Sandboxes) Args() []string

Args is the argument vector the most recent PROGRAM was given — what followed `sh -c <line> sh`. It is how a test checks that a caller's args reached the program and not, say, the compiler that built it.

func (*Sandboxes) Lines

func (s *Sandboxes) Lines() []string

Lines is every shell line the peer was asked to run, in order, for a test that asserts on what was SENT rather than only on what came back.

func (*Sandboxes) Live

func (s *Sandboxes) Live() int

Live is how many leases the peer still holds. A code-interpreter session legitimately outlives its run; a function invoke must leave none.

func (*Sandboxes) Pod

func (s *Sandboxes) Pod(id string) *Pod

Pod is the fake sandbox with this id, or nil.

func (*Sandboxes) Ran

func (s *Sandboxes) Ran() int32

Ran counts PROGRAMS, not calls. Every test that asks "did compute happen" means this: a lease nothing ran in is not compute, and the artifact sweep that follows a run is bookkeeping — counting either would double the answer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL