feat: add FAQ uncovered workflow

This commit is contained in:
姚章浩
2026-07-27 11:54:34 +08:00
parent e582d851a4
commit aa0513eb59
28 changed files with 1319 additions and 274 deletions
+8 -3
View File
@@ -152,9 +152,15 @@ type tokenResp struct {
ExpireIn int64 `json:"expireIn"`
}
// InsertRecord sends a single row to the DingTalk Notable v1 base.
// It returns the record ID on success.
// InsertRecord sends a single question-table row to DingTalk Notable v1.
// It remains the compatibility entry point for the existing question-table dispatcher.
func (c *Client) InsertRecord(ctx context.Context, row Row) (string, error) {
return c.InsertFields(ctx, row.ToFields())
}
// InsertFields sends one already-typed field map to DingTalk Notable v1 and
// returns the record ID. It is shared by other reviewed Notable table schemas.
func (c *Client) InsertFields(ctx context.Context, fields map[string]interface{}) (string, error) {
token, err := c.getAccessToken(ctx)
if err != nil {
return "", fmt.Errorf("questiontable token: %w", err)
@@ -164,7 +170,6 @@ func (c *Client) InsertRecord(ctx context.Context, row Row) (string, error) {
url.PathEscape(c.baseID), url.PathEscape(c.sheetID))
reqURL := c.baseURL + path + "?operatorId=" + url.QueryEscape(c.operatorID)
fields := row.ToFields()
body := insertRecordReq{Records: []insertRecord{{Fields: fields}}}
jsonBody, err := json.Marshal(body)
if err != nil {
@@ -0,0 +1,52 @@
package questiontable
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestInsertFieldsUsesExactProvidedFAQFieldMap(t *testing.T) {
want := map[string]interface{}{
"01ZM8y7": "标准问题",
"fj3JQ1C": "原始问题",
"07YYagS": "Chinese",
}
var got map[string]interface{}
mux := http.NewServeMux()
mux.HandleFunc("/token", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"accessToken":"token","expireIn":7200}`))
})
mux.HandleFunc("/v1.0/notable/bases/faq-base/sheets/faq-sheet/records", func(w http.ResponseWriter, r *http.Request) {
var body struct {
Records []struct {
Fields map[string]interface{} `json:"fields"`
} `json:"records"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || len(body.Records) != 1 {
t.Fatalf("invalid request body: err=%v body=%#v", err, body)
}
got = body.Records[0].Fields
_, _ = w.Write([]byte(`{"value":[{"id":"faq-record-1"}]}`))
})
server := httptest.NewServer(mux)
defer server.Close()
client, err := NewClient(Config{
ClientID: "client", Secret: "secret", BaseID: "faq-base", SheetID: "faq-sheet", OperatorID: "operator",
TokenURL: server.URL + "/token", BaseURL: server.URL,
}, server.Client())
if err != nil {
t.Fatal(err)
}
recordID, err := client.InsertFields(context.Background(), want)
if err != nil {
t.Fatal(err)
}
if recordID != "faq-record-1" || !reflect.DeepEqual(got, want) {
t.Fatalf("recordID=%q fields=%#v", recordID, got)
}
}