Files
openchamber-android-native/third_party/mygo/plugins/sqlite/sqlite_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

146 lines
4.1 KiB
Go

package sqlite
import (
"context"
"encoding/json/v2"
"errors"
"math"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/egoist/mygo"
"github.com/egoist/mygo/internal/wait"
)
func TestPluginBindings(t *testing.T) {
mygo.Use(New(Options{Directory: t.TempDir()}))
ts, err := mygo.GenerateTypeScript()
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(ts), "plugin:sqlite") {
t.Fatal("plugin leaked into generated client")
}
}
func TestPageOwnershipAndCleanup(t *testing.T) {
needSQLite(t)
s := New(Options{Directory: t.TempDir()}).Service.(*service)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
id, err := s.open(ctx, 1, ":memory:", openOptions{})
if err != nil {
t.Fatal(err)
}
db, err := s.database(1, id)
if err != nil {
t.Fatal(err)
}
if _, err := s.database(2, id); err == nil {
t.Fatal("another window got the connection")
}
if err := s.close(key{2, id}); err != nil {
t.Fatal(err)
}
execute(t, db, "CREATE TABLE items(n)")
cancel()
if _, err := s.database(1, id); err == nil {
t.Fatal("a canceled page retained access before asynchronous cleanup")
}
select {
case <-db.life.Done():
case <-time.After(wait.Guard):
t.Fatal("page navigation did not close connection")
}
if _, err := db.Query(context.Background(), "SELECT 1"); !errors.Is(err, ErrClosed) {
t.Fatal(err)
}
id, err = s.open(context.Background(), 1, ":memory:", openOptions{})
if err != nil {
t.Fatal(err)
}
db, _ = s.database(1, id)
s.shutdown()
if _, err := db.Query(context.Background(), "SELECT 1"); !errors.Is(err, ErrClosed) {
t.Fatal(err)
}
if _, err := s.open(context.Background(), 1, ":memory:", openOptions{}); !errors.Is(err, ErrClosed) {
t.Fatal(err)
}
}
func TestPageDatabaseNames(t *testing.T) {
for _, name := range []string{"", ".", "..", "../a.db", "/tmp/a.db", `C:\a.db`, `dir\a.db`, "a\x00b", "CON.db", "nul", "LPT1.db", "a.db.", " a.db", "file:other.db"} {
if err := databaseName(name); err == nil {
t.Errorf("accepted %q", name)
}
}
for _, name := range []string{"app.db", "中文.db", "company.db", "a-b.db"} {
if err := databaseName(name); err != nil {
t.Errorf("%q: %v", name, err)
}
}
dir := t.TempDir()
path := filepath.Join(dir, "link.db")
if err := os.Symlink(filepath.Join(t.TempDir(), "outside.db"), path); err == nil {
s := New(Options{Directory: dir}).Service.(*service)
if _, err := s.open(context.Background(), 1, "link.db", openOptions{}); err == nil || !strings.Contains(err.Error(), "symbolic link") {
t.Fatal(err)
}
}
}
func TestWireRoundTrip(t *testing.T) {
values := []any{nil, int64(-9223372036854775808), int64(9223372036854775807), 0.125, "中文\x00", []byte{0, 255}, []byte{}, "", float64(0)}
wire := rowsWire(Rows{Values: [][]any{values}})
data, err := json.Marshal(wire)
if err != nil {
t.Fatal(err)
}
var decoded wireRows
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatal(err)
}
got, err := parameters(decoded.Rows[0])
if err != nil {
t.Fatal(err)
}
// Omitted empty BLOB fields decode as a nil []byte, still an empty BLOB.
got[6] = []byte{}
if !reflect.DeepEqual(got, values) {
t.Fatalf("got %#v, want %#v", got, values)
}
for _, value := range []wireValue{{Type: "integer", Integer: "9223372036854775808"}, {Type: "integer"}, {Type: "oops"}} {
if _, err := parameters([]wireValue{value}); err == nil {
t.Fatal("invalid value accepted", value)
}
}
}
func TestInfiniteQueryResult(t *testing.T) {
db := testDB(t, ":memory:")
rows := query(t, db, "SELECT 1e999, -1e999")
if !math.IsInf(rows.Values[0][0].(float64), 1) || !math.IsInf(rows.Values[0][1].(float64), -1) {
t.Fatal("lost SQLite's infinite REAL values", rows)
}
wire := rowsWire(rows)
data, err := json.Marshal(wire)
if err != nil {
t.Fatal(err)
}
var decoded wireRows
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatal(err)
}
if decoded.Rows[0][0].Special != "Infinity" || decoded.Rows[0][1].Special != "-Infinity" {
t.Fatal(string(data))
}
if _, err := parameters(decoded.Rows[0]); err == nil {
t.Fatal("nonfinite parameters should still be rejected")
}
}