From e22c33ee6d8dbdf14ede219aa32eb31ccea6ad11 Mon Sep 17 00:00:00 2001 From: Egutierrez Date: Tue, 2 Jun 2026 23:48:59 +0200 Subject: [PATCH] refactor(infra): split de drivers pesados a subpaquetes + fix TestSSEHandler Mueve duckdb_open, clickhouse_open, postgres_open, matrix_* y keyring_token_store del paquete monolitico functions/infra a subpaquetes propios (functions/infra/{duckdb,clickhouse,postgres,matrix,keyring}). El paquete infra ya no importa los drivers (go-duckdb, clickhouse-go, pgx, mautrix, go-keyring), por lo que las apps que solo usan funciones ligeras (process, cron, http, sqlite) dejan de arrastrarlos. Reduccion de binarios: dag_engine 72->10MB, registry_api 70->8.7MB, services_api 70->9MB, call_monitor 68->6.6MB, sqlite_api 70->8.9MB. Los IDs del registry se mantienen estables (domain: infra en frontmatter). Se preservan los build tags goolm/libolm de matrix_crypto_init. Tambien corrige TestSSEHandler: el test leia el body con un unico Read() que con HTTP chunked solo capturaba el primer evento; ahora usa io.ReadAll hasta EOF. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../infra/{ => clickhouse}/clickhouse_open.go | 4 +--- .../infra/{ => clickhouse}/clickhouse_open.md | 2 +- functions/infra/{ => duckdb}/duckdb_open.go | 4 +--- functions/infra/{ => duckdb}/duckdb_open.md | 2 +- .../infra/{ => keyring}/keyring_token_store.go | 4 ++-- .../infra/{ => keyring}/keyring_token_store.md | 4 ++-- .../{ => keyring}/keyring_token_store_test.go | 2 +- .../infra/{ => matrix}/matrix_client_init.go | 2 +- .../infra/{ => matrix}/matrix_client_init.md | 4 ++-- .../{ => matrix}/matrix_client_init_test.go | 2 +- .../infra/{ => matrix}/matrix_crypto_init.go | 2 +- .../infra/{ => matrix}/matrix_crypto_init.md | 4 ++-- .../{ => matrix}/matrix_crypto_init_test.go | 16 ++++++++-------- .../infra/{ => matrix}/matrix_message_send.go | 2 +- .../infra/{ => matrix}/matrix_message_send.md | 4 ++-- .../{ => matrix}/matrix_message_send_test.go | 2 +- functions/infra/{ => matrix}/matrix_room_list.go | 6 +++--- functions/infra/{ => matrix}/matrix_room_list.md | 4 ++-- .../infra/{ => matrix}/matrix_room_list_test.go | 8 ++++---- .../infra/{ => matrix}/matrix_sync_service.go | 2 +- .../infra/{ => matrix}/matrix_sync_service.md | 4 ++-- .../{ => matrix}/matrix_sync_service_test.go | 4 ++-- functions/infra/{ => postgres}/postgres_open.go | 2 +- functions/infra/{ => postgres}/postgres_open.md | 2 +- functions/infra/sse_test.go | 14 +++++++++++--- 25 files changed, 55 insertions(+), 51 deletions(-) rename functions/infra/{ => clickhouse}/clickhouse_open.go (94%) rename functions/infra/{ => clickhouse}/clickhouse_open.md (96%) rename functions/infra/{ => duckdb}/duckdb_open.go (94%) rename functions/infra/{ => duckdb}/duckdb_open.md (95%) rename functions/infra/{ => keyring}/keyring_token_store.go (96%) rename functions/infra/{ => keyring}/keyring_token_store.md (97%) rename functions/infra/{ => keyring}/keyring_token_store_test.go (99%) rename functions/infra/{ => matrix}/matrix_client_init.go (99%) rename functions/infra/{ => matrix}/matrix_client_init.md (97%) rename functions/infra/{ => matrix}/matrix_client_init_test.go (99%) rename functions/infra/{ => matrix}/matrix_crypto_init.go (99%) rename functions/infra/{ => matrix}/matrix_crypto_init.md (97%) rename functions/infra/{ => matrix}/matrix_crypto_init_test.go (96%) rename functions/infra/{ => matrix}/matrix_message_send.go (99%) rename functions/infra/{ => matrix}/matrix_message_send.md (97%) rename functions/infra/{ => matrix}/matrix_message_send_test.go (99%) rename functions/infra/{ => matrix}/matrix_room_list.go (98%) rename functions/infra/{ => matrix}/matrix_room_list.md (96%) rename functions/infra/{ => matrix}/matrix_room_list_test.go (97%) rename functions/infra/{ => matrix}/matrix_sync_service.go (99%) rename functions/infra/{ => matrix}/matrix_sync_service.md (96%) rename functions/infra/{ => matrix}/matrix_sync_service_test.go (99%) rename functions/infra/{ => postgres}/postgres_open.go (98%) rename functions/infra/{ => postgres}/postgres_open.md (96%) diff --git a/functions/infra/clickhouse_open.go b/functions/infra/clickhouse/clickhouse_open.go similarity index 94% rename from functions/infra/clickhouse_open.go rename to functions/infra/clickhouse/clickhouse_open.go index d9549193..2ff17622 100644 --- a/functions/infra/clickhouse_open.go +++ b/functions/infra/clickhouse/clickhouse_open.go @@ -1,6 +1,4 @@ -//go:build !noclickhouse - -package infra +package clickhouse import ( "database/sql" diff --git a/functions/infra/clickhouse_open.md b/functions/infra/clickhouse/clickhouse_open.md similarity index 96% rename from functions/infra/clickhouse_open.md rename to functions/infra/clickhouse/clickhouse_open.md index a8106d04..17075fef 100644 --- a/functions/infra/clickhouse_open.md +++ b/functions/infra/clickhouse/clickhouse_open.md @@ -29,7 +29,7 @@ output: "conexion sql.DB abierta a ClickHouse con ping verificado" tested: false tests: [] test_file_path: "" -file_path: "functions/infra/clickhouse_open.go" +file_path: "functions/infra/clickhouse/clickhouse_open.go" --- ## Ejemplo diff --git a/functions/infra/duckdb_open.go b/functions/infra/duckdb/duckdb_open.go similarity index 94% rename from functions/infra/duckdb_open.go rename to functions/infra/duckdb/duckdb_open.go index 7870b8dd..4e896dde 100644 --- a/functions/infra/duckdb_open.go +++ b/functions/infra/duckdb/duckdb_open.go @@ -1,6 +1,4 @@ -//go:build !noduckdb - -package infra +package duckdb import ( "database/sql" diff --git a/functions/infra/duckdb_open.md b/functions/infra/duckdb/duckdb_open.md similarity index 95% rename from functions/infra/duckdb_open.md rename to functions/infra/duckdb/duckdb_open.md index 63ddd7bd..5b6a7b79 100644 --- a/functions/infra/duckdb_open.md +++ b/functions/infra/duckdb/duckdb_open.md @@ -21,7 +21,7 @@ output: "conexion sql.DB abierta a DuckDB" tested: false tests: [] test_file_path: "" -file_path: "functions/infra/duckdb_open.go" +file_path: "functions/infra/duckdb/duckdb_open.go" --- ## Ejemplo diff --git a/functions/infra/keyring_token_store.go b/functions/infra/keyring/keyring_token_store.go similarity index 96% rename from functions/infra/keyring_token_store.go rename to functions/infra/keyring/keyring_token_store.go index a5584384..8c613d05 100644 --- a/functions/infra/keyring_token_store.go +++ b/functions/infra/keyring/keyring_token_store.go @@ -1,4 +1,4 @@ -package infra +package keyring import ( "encoding/json" @@ -20,7 +20,7 @@ type Token struct { UserID string `json:"user_id"` DeviceID string `json:"device_id,omitempty"` HomeserverURL string `json:"homeserver_url"` - Issuer string `json:"issuer,omitempty"` // MAS/OIDC issuer URL + Issuer string `json:"issuer,omitempty"` // MAS/OIDC issuer URL ClientID string `json:"client_id,omitempty"` // MAS client_id used } diff --git a/functions/infra/keyring_token_store.md b/functions/infra/keyring/keyring_token_store.md similarity index 97% rename from functions/infra/keyring_token_store.md rename to functions/infra/keyring/keyring_token_store.md index b0996e0b..5610a3a8 100644 --- a/functions/infra/keyring_token_store.md +++ b/functions/infra/keyring/keyring_token_store.md @@ -54,8 +54,8 @@ tests: - "Save then Delete then Load returns ErrNotFound" - "Delete nonexistent is idempotent" - "Save twice overwrites with second token" -test_file_path: "functions/infra/keyring_token_store_test.go" -file_path: "functions/infra/keyring_token_store.go" +test_file_path: "functions/infra/keyring/keyring_token_store_test.go" +file_path: "functions/infra/keyring/keyring_token_store.go" --- ## Ejemplo diff --git a/functions/infra/keyring_token_store_test.go b/functions/infra/keyring/keyring_token_store_test.go similarity index 99% rename from functions/infra/keyring_token_store_test.go rename to functions/infra/keyring/keyring_token_store_test.go index 1b1ebbb9..1c32526f 100644 --- a/functions/infra/keyring_token_store_test.go +++ b/functions/infra/keyring/keyring_token_store_test.go @@ -1,4 +1,4 @@ -package infra +package keyring import ( "errors" diff --git a/functions/infra/matrix_client_init.go b/functions/infra/matrix/matrix_client_init.go similarity index 99% rename from functions/infra/matrix_client_init.go rename to functions/infra/matrix/matrix_client_init.go index 513767a7..3c82bcc5 100644 --- a/functions/infra/matrix_client_init.go +++ b/functions/infra/matrix/matrix_client_init.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "context" diff --git a/functions/infra/matrix_client_init.md b/functions/infra/matrix/matrix_client_init.md similarity index 97% rename from functions/infra/matrix_client_init.md rename to functions/infra/matrix/matrix_client_init.md index 404d5428..0d820565 100644 --- a/functions/infra/matrix_client_init.md +++ b/functions/infra/matrix/matrix_client_init.md @@ -38,8 +38,8 @@ tests: - "Whoami 401 token invalido" - "EnableCrypto true devuelve error not implemented" - "StoreDir se crea con permisos 0700" -test_file_path: "functions/infra/matrix_client_init_test.go" -file_path: "functions/infra/matrix_client_init.go" +test_file_path: "functions/infra/matrix/matrix_client_init_test.go" +file_path: "functions/infra/matrix/matrix_client_init.go" --- ## Ejemplo diff --git a/functions/infra/matrix_client_init_test.go b/functions/infra/matrix/matrix_client_init_test.go similarity index 99% rename from functions/infra/matrix_client_init_test.go rename to functions/infra/matrix/matrix_client_init_test.go index da822d89..3e34e224 100644 --- a/functions/infra/matrix_client_init_test.go +++ b/functions/infra/matrix/matrix_client_init_test.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "encoding/json" diff --git a/functions/infra/matrix_crypto_init.go b/functions/infra/matrix/matrix_crypto_init.go similarity index 99% rename from functions/infra/matrix_crypto_init.go rename to functions/infra/matrix/matrix_crypto_init.go index a6bf74d9..600f26f3 100644 --- a/functions/infra/matrix_crypto_init.go +++ b/functions/infra/matrix/matrix_crypto_init.go @@ -1,6 +1,6 @@ //go:build goolm || libolm -package infra +package matrix import ( "context" diff --git a/functions/infra/matrix_crypto_init.md b/functions/infra/matrix/matrix_crypto_init.md similarity index 97% rename from functions/infra/matrix_crypto_init.md rename to functions/infra/matrix/matrix_crypto_init.md index 9200ae5f..88515a43 100644 --- a/functions/infra/matrix_crypto_init.md +++ b/functions/infra/matrix/matrix_crypto_init.md @@ -38,8 +38,8 @@ tests: - "directorio del store se crea con permisos 0700" - "input valido Init exito helper no nil" - "Synapse 401 en keys upload devuelve error" -test_file_path: "functions/infra/matrix_crypto_init_test.go" -file_path: "functions/infra/matrix_crypto_init.go" +test_file_path: "functions/infra/matrix/matrix_crypto_init_test.go" +file_path: "functions/infra/matrix/matrix_crypto_init.go" --- ## Ejemplo diff --git a/functions/infra/matrix_crypto_init_test.go b/functions/infra/matrix/matrix_crypto_init_test.go similarity index 96% rename from functions/infra/matrix_crypto_init_test.go rename to functions/infra/matrix/matrix_crypto_init_test.go index b8b920af..7699eb61 100644 --- a/functions/infra/matrix_crypto_init_test.go +++ b/functions/infra/matrix/matrix_crypto_init_test.go @@ -1,6 +1,6 @@ //go:build goolm || libolm -package infra +package matrix import ( "context" @@ -72,10 +72,10 @@ func newSynapseMock(t *testing.T, uploadStatus int) *httptest.Server { mux.HandleFunc("/_matrix/client/v3/keys/query", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") resp := map[string]any{ - "device_keys": map[string]any{}, - "failures": map[string]any{}, - "master_keys": map[string]any{}, - "user_signing_keys": map[string]any{}, + "device_keys": map[string]any{}, + "failures": map[string]any{}, + "master_keys": map[string]any{}, + "user_signing_keys": map[string]any{}, "self_signing_keys": map[string]any{}, } _ = json.NewEncoder(w).Encode(resp) @@ -85,9 +85,9 @@ func newSynapseMock(t *testing.T, uploadStatus int) *httptest.Server { mux.HandleFunc("/_matrix/client/v3/sync", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") resp := map[string]any{ - "next_batch": "s0_1", - "rooms": map[string]any{}, - "to_device": map[string]any{"events": []any{}}, + "next_batch": "s0_1", + "rooms": map[string]any{}, + "to_device": map[string]any{"events": []any{}}, "device_one_time_keys_count": map[string]any{}, } _ = json.NewEncoder(w).Encode(resp) diff --git a/functions/infra/matrix_message_send.go b/functions/infra/matrix/matrix_message_send.go similarity index 99% rename from functions/infra/matrix_message_send.go rename to functions/infra/matrix/matrix_message_send.go index ce4ba5cf..3c21810d 100644 --- a/functions/infra/matrix_message_send.go +++ b/functions/infra/matrix/matrix_message_send.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "bytes" diff --git a/functions/infra/matrix_message_send.md b/functions/infra/matrix/matrix_message_send.md similarity index 97% rename from functions/infra/matrix_message_send.md rename to functions/infra/matrix/matrix_message_send.md index f4d075bb..c7c47b94 100644 --- a/functions/infra/matrix_message_send.md +++ b/functions/infra/matrix/matrix_message_send.md @@ -53,8 +53,8 @@ tests: - "SendReply client nil devuelve error" - "EditMessage client nil devuelve error" - "SendReaction client nil devuelve error" -test_file_path: "functions/infra/matrix_message_send_test.go" -file_path: "functions/infra/matrix_message_send.go" +test_file_path: "functions/infra/matrix/matrix_message_send_test.go" +file_path: "functions/infra/matrix/matrix_message_send.go" --- ## Ejemplo diff --git a/functions/infra/matrix_message_send_test.go b/functions/infra/matrix/matrix_message_send_test.go similarity index 99% rename from functions/infra/matrix_message_send_test.go rename to functions/infra/matrix/matrix_message_send_test.go index aa494e73..6bbff06a 100644 --- a/functions/infra/matrix_message_send_test.go +++ b/functions/infra/matrix/matrix_message_send_test.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "context" diff --git a/functions/infra/matrix_room_list.go b/functions/infra/matrix/matrix_room_list.go similarity index 98% rename from functions/infra/matrix_room_list.go rename to functions/infra/matrix/matrix_room_list.go index 216d279b..e1561725 100644 --- a/functions/infra/matrix_room_list.go +++ b/functions/infra/matrix/matrix_room_list.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "context" @@ -23,8 +23,8 @@ type RoomSummary struct { IsSpace bool `json:"is_space"` // m.room.type == m.space IsEncrypted bool `json:"is_encrypted"` // m.room.encryption state event presente MemberCount int `json:"member_count"` - LastEventTs int64 `json:"last_event_ts"` // unix ms del ultimo evento conocido - UnreadCount int `json:"unread_count"` // notifications.unread + highlight + LastEventTs int64 `json:"last_event_ts"` // unix ms del ultimo evento conocido + UnreadCount int `json:"unread_count"` // notifications.unread + highlight Tags []string `json:"tags,omitempty"` // m.tag account_data } diff --git a/functions/infra/matrix_room_list.md b/functions/infra/matrix/matrix_room_list.md similarity index 96% rename from functions/infra/matrix_room_list.md rename to functions/infra/matrix/matrix_room_list.md index 08f62d03..c5cd4fde 100644 --- a/functions/infra/matrix_room_list.md +++ b/functions/infra/matrix/matrix_room_list.md @@ -30,8 +30,8 @@ tests: - "IsDirect set correctamente segun m.direct" - "IsEncrypted set segun presencia de m.room.encryption" - "client nil devuelve error" -test_file_path: "functions/infra/matrix_room_list_test.go" -file_path: "functions/infra/matrix_room_list.go" +test_file_path: "functions/infra/matrix/matrix_room_list_test.go" +file_path: "functions/infra/matrix/matrix_room_list.go" --- ## Ejemplo diff --git a/functions/infra/matrix_room_list_test.go b/functions/infra/matrix/matrix_room_list_test.go similarity index 97% rename from functions/infra/matrix_room_list_test.go rename to functions/infra/matrix/matrix_room_list_test.go index a9d70230..48c6f28a 100644 --- a/functions/infra/matrix_room_list_test.go +++ b/functions/infra/matrix/matrix_room_list_test.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "context" @@ -18,9 +18,9 @@ import ( // los handlers lo decodifican antes de hacer lookup. type matrixTestServer struct { *httptest.Server - joinedRooms []string // room IDs que devuelve /joined_rooms - roomNames map[string]string // roomID -> name (no seteado = sin m.room.name) - encryptedRooms map[string]bool // roomID -> tiene encryption event + joinedRooms []string // room IDs que devuelve /joined_rooms + roomNames map[string]string // roomID -> name (no seteado = sin m.room.name) + encryptedRooms map[string]bool // roomID -> tiene encryption event directContent map[string][]string // userID -> []roomID roomTags map[string][]string // roomID -> []tag names } diff --git a/functions/infra/matrix_sync_service.go b/functions/infra/matrix/matrix_sync_service.go similarity index 99% rename from functions/infra/matrix_sync_service.go rename to functions/infra/matrix/matrix_sync_service.go index 4a32a996..f6ae6d16 100644 --- a/functions/infra/matrix_sync_service.go +++ b/functions/infra/matrix/matrix_sync_service.go @@ -1,4 +1,4 @@ -package infra +package matrix import ( "context" diff --git a/functions/infra/matrix_sync_service.md b/functions/infra/matrix/matrix_sync_service.md similarity index 96% rename from functions/infra/matrix_sync_service.md rename to functions/infra/matrix/matrix_sync_service.md index 7a07717d..dc409f0d 100644 --- a/functions/infra/matrix_sync_service.md +++ b/functions/infra/matrix/matrix_sync_service.md @@ -36,8 +36,8 @@ tests: - "Error401NoExit" - "StopIdempotente" - "CtxCancelCierraChannels" -test_file_path: "functions/infra/matrix_sync_service_test.go" -file_path: "functions/infra/matrix_sync_service.go" +test_file_path: "functions/infra/matrix/matrix_sync_service_test.go" +file_path: "functions/infra/matrix/matrix_sync_service.go" --- ## Ejemplo diff --git a/functions/infra/matrix_sync_service_test.go b/functions/infra/matrix/matrix_sync_service_test.go similarity index 99% rename from functions/infra/matrix_sync_service_test.go rename to functions/infra/matrix/matrix_sync_service_test.go index e8063509..8e37ecd4 100644 --- a/functions/infra/matrix_sync_service_test.go +++ b/functions/infra/matrix/matrix_sync_service_test.go @@ -1,6 +1,6 @@ //go:build goolm -package infra +package matrix import ( "context" @@ -163,7 +163,7 @@ func TestMatrixSyncService_BackoffRecovery(t *testing.T) { cli := newTestSyncClient(t, srv.URL) h, err := MatrixSyncService(context.Background(), MatrixSyncServiceConfig{ Client: cli, - InitialBackoffMS: 50, // backoff corto para tests + InitialBackoffMS: 50, // backoff corto para tests MaxBackoffMS: 200, ChannelBuffer: 16, }) diff --git a/functions/infra/postgres_open.go b/functions/infra/postgres/postgres_open.go similarity index 98% rename from functions/infra/postgres_open.go rename to functions/infra/postgres/postgres_open.go index 548ee491..e9454644 100644 --- a/functions/infra/postgres_open.go +++ b/functions/infra/postgres/postgres_open.go @@ -1,4 +1,4 @@ -package infra +package postgres import ( "database/sql" diff --git a/functions/infra/postgres_open.md b/functions/infra/postgres/postgres_open.md similarity index 96% rename from functions/infra/postgres_open.md rename to functions/infra/postgres/postgres_open.md index 79759d94..852dbff6 100644 --- a/functions/infra/postgres_open.md +++ b/functions/infra/postgres/postgres_open.md @@ -31,7 +31,7 @@ output: "conexion sql.DB abierta a PostgreSQL" tested: false tests: [] test_file_path: "" -file_path: "functions/infra/postgres_open.go" +file_path: "functions/infra/postgres/postgres_open.go" --- ## Ejemplo diff --git a/functions/infra/sse_test.go b/functions/infra/sse_test.go index 62740a61..0336db07 100644 --- a/functions/infra/sse_test.go +++ b/functions/infra/sse_test.go @@ -2,6 +2,7 @@ package infra import ( "context" + "io" "net/http" "net/http/httptest" "strings" @@ -210,9 +211,16 @@ func TestSSEHandler(t *testing.T) { } defer resp.Body.Close() - buf := make([]byte, 4096) - n, _ := resp.Body.Read(buf) - body := string(buf[:n]) + // Leer el body completo hasta EOF. El canal se cierra antes de la + // peticion, asi que el handler envia ambos eventos y termina, cerrando + // el stream. Un unico Read podria devolver solo el primer chunk + // (event: first), porque io.Reader.Read no garantiza llenar el buffer; + // io.ReadAll consume todos los chunks emitidos por el handler. + raw, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + body := string(raw) for _, want := range []string{"event: first", "event: second", "data: 1", "data: 2"} { if !strings.Contains(body, want) {