oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package team
2
3import (
4 "database/sql"
5 "math"
6 "strings"
7 "testing"
8)
9
10func TestPeriodQueriesBoundedHistory(t *testing.T) {
11 e := newEnv(t)
12 must(t, e.store.ConfigureBudgets(e.ctx, acctA, 0, periodSet()))
13 s, h := periodJob(e, 1)
14 id, err := e.store.Intend(e.ctx, s, h, intent("seed", Usage{InputTokens: 7, OutputTokens: 3}))
15 must(t, err)
16 a, err := e.store.GetAction(e.ctx, s, id)
17 must(t, err)
18 _, err = e.store.Intend(e.ctx, s, h, intent("current", Usage{InputTokens: 2, OutputTokens: 1}))
19 must(t, err)
20 must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error {
21 _, err := tx.Exec(`UPDATE reservations SET period='2020-01',created_at='2020-01-01T00:00:00Z' WHERE id=?`, a.Reservation)
22 if err != nil {
23 return err
24 }
25 _, err = tx.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<100000)
26 INSERT INTO reservations(account,job_id,step_id,attempt_id,action_id,r_calls,r_input,r_output,r_cost,s_calls,s_input,s_output,s_cost,state,receipt_id,settled_by,created_at,settled_at,project_id,period)
27 SELECT account,job_id,step_id,attempt_id,0,1,7,3,0,1,7,3,0,'settled','','','2020-01-01T00:00:00Z','2020-01-01T00:00:00Z',1,'2020-01' FROM reservations,n WHERE id=?`, a.Reservation)
28 if err != nil {
29 return err
30 }
31 _, err = tx.Exec(`UPDATE reservations SET state='settled_audited',s_calls=1,s_input=2,s_output=1 WHERE period!='2020-01'`)
32 if err != nil {
33 return err
34 }
35 _, err = tx.Exec(`UPDATE time_reservations SET period='2020-01'`)
36 return err
37 }))
38 for _, project := range []ProjectID{0, 1} {
39 must(t, e.store.read(e.ctx, func(tx *sql.Tx) error {
40 for _, table := range []string{"reservations", "time_reservations"} {
41 for _, settled := range []bool{false, true} {
42 q, args := periodQuery(table, acctA, project, e.clock.Now(), settled)
43 rows, err := tx.QueryContext(e.ctx, "EXPLAIN QUERY PLAN "+q, args...)
44 if err != nil {
45 return err
46 }
47 var plan string
48 for rows.Next() {
49 var a, b, c int
50 var detail string
51 if err := rows.Scan(&a, &b, &c, &detail); err != nil {
52 return err
53 }
54 plan += detail
55 }
56 err = rows.Err()
57 rows.Close()
58 if err != nil {
59 return err
60 }
61 if !strings.Contains(plan, "SEARCH") || !strings.Contains(plan, "state=?") || !strings.Contains(plan, "period=?") || (project != 0 && !strings.Contains(plan, "project_id=?")) {
62 t.Fatalf("bounded-history query plan: %s", plan)
63 }
64 var count int
65 if err := tx.QueryRowContext(e.ctx, "SELECT count(*) FROM ("+q+")", args...).Scan(&count); err != nil {
66 return err
67 }
68 if count > 2 {
69 t.Fatalf("bounded-history query visited %d rows", count)
70 }
71 }
72 }
73 return nil
74 }))
75 u := periodUsage(e, project)
76 if u.Reserved != (PeriodLimits{}) || u.Spent.Calls != 1 || u.Spent.InputTokens != 2 {
77 t.Fatal("old rows charged to current month", u)
78 }
79 }
80 must(t, e.store.tx(e.ctx, func(tx *sql.Tx) error {
81 _, err := tx.Exec(`UPDATE reservations SET state='settled',s_calls=1,s_input=?,period=? WHERE id IN (?,(SELECT max(id) FROM reservations))`, math.MaxInt64, period(e.clock.Now()), a.Reservation)
82 return err
83 }))
84 _, err = e.store.PeriodUsage(e.ctx, acctA, 0, e.clock.Now())
85 wantCode(t, err, CodeOverflow)
86}
87
88func TestPeriodQueriesBackfill(t *testing.T) {
89 for _, tc := range []struct {
90 name, stamp, month string
91 bad bool
92 }{{"valid", "2026-11-01T00:00:00Z", "2026-11", false}, {"fraction at month end", "2026-10-31T23:59:59.999999999Z", "2026-10", false}, {"invalid", "invalid", "", true}, {"invalid date", "2026-02-31T00:00:00Z", "", true}, {"missing time", "2026-11-01", "", true}} {
93 t.Run(tc.name, func(t *testing.T) {
94 e := newEnv(t)
95 s, h := periodJob(e, 1)
96 _, err := e.store.Intend(e.ctx, s, h, intent("old", Usage{InputTokens: 2}))
97 must(t, err)
98 must(t, e.store.Close())
99 e.store = nil
100 db, err := sql.Open("sqlite", e.path)
101 must(t, err)
102 defer db.Close()
103 dropRunnerPolicySchema(t, db)
104 _, err = db.Exec("UPDATE service SET schema_version=4")
105 must(t, err)
106 _, err = db.Exec(`UPDATE reservations SET created_at=?`, tc.stamp)
107 must(t, err)
108 core, err := Open(e.ctx, e.path, e.options())
109 if tc.bad {
110 if err == nil {
111 core.Close()
112 t.Fatal("invalid timestamp backfilled")
113 }
114 return
115 }
116 must(t, err)
117 defer core.Close()
118 var project int
119 var month string
120 must(t, db.QueryRow(`SELECT project_id,period FROM reservations`).Scan(&project, &month))
121 if project != 1 || month != tc.month {
122 t.Fatal("wrong backfill", project, month)
123 }
124 })
125 }
126}
127
128func TestPeriodQueriesSharedIndexes(t *testing.T) {
129 for _, index := range []string{"reservations_period_account", "reservations_period_project", "time_period_account", "time_period_project", "recovery_charges_scope"} {
130 t.Run(index, func(t *testing.T) {
131 e, writer := sharedFixture(t)
132 _, err := writer.Exec("DROP INDEX " + index)
133 must(t, err)
134 core, err := OpenShared(e.ctx, writer, e.path, e.options())
135 if err == nil {
136 core.Close()
137 t.Fatal("attachment accepted missing period index")
138 }
139 wantCode(t, err, CodeStaleStore)
140 })
141 }
142}
143