mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 14:02:43 +08:00
Image uploads to a spreadsheet hard-coded parent_type=sheet_image at every entry point. Imported "office" spreadsheets carry a token prefixed with "fake_office_", for which the drive backend requires parent_type=office_sheet_file. Funnel the parent_type selection through a single sheets-domain helper so the rule lives in one place and every image-upload path (float-image, +cells-set-image, backward +media-upload, and every dry-run preview) stays consistent. - Add sheetMediaParentType(token) in the sheets domain: returns office_sheet_file for fake_office_-prefixed tokens, otherwise sheet_image. - Add an uploadSheetImage(...) collector that builds the DriveMediaUploadAllConfig (including parent_type) once, replacing the per-call-site hand-rolled configs. - Route both main-domain image entries through the collector — float-image local upload and +cells-set-image — covering Execute and the dry-run preview body/desc. - Cover the backward +media-upload entry: single-part, multipart (>20MB), and both dry-run bodies. backward is a separate package and an intentional verbatim mirror of shortcuts/sheets/, so it keeps its own copy of the helper rather than importing the main domain. - Leave the shared common.UploadDriveMediaAllTyped upload layer untouched — the fake_office_ rule is sheets-specific and must not leak into mail/slides/doc/drive/base. Tests: - Pure-function TestSheetMediaParentType (5 cases incl. prefix-only and mid-string non-match). - Main-domain dry-run TestCellsSetImage_DryRunOfficeParentType and TestUploadSheetImage_ParentType / _FileOpenError that exercise the Execute path on the wire, asserting parent_type via the captured multipart body and typed validation metadata (errs.ProblemOf category/subtype, fs.ErrNotExist cause preserved) on file open errors. decodeSheetMediaMultipartBody fails fast on NextPart / ReadFrom errors rather than silently producing a partial body. - backward TestSheetMediaUploadExecuteOfficeParentType (real multipart wire) and TestSheetMediaUploadDryRunSmallFileOfficeParentType (small-file dry-run preview for fake_office_). - cli_e2e tests/cli_e2e/sheets/sheets_image_upload_dryrun_test.go: --dry-run end-to-end across +media-upload and +cells-set-image, native and fake_office_ tokens, asserting api.0 is POST upload_all with parent_type=sheet_image / office_sheet_file and parent_node = token.
113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package sheets
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
clie2e "github.com/larksuite/cli/tests/cli_e2e"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// TestSheets_ImageUploadDryRunParentType pins the parent_type the sheets
|
|
// image-upload shortcuts emit in --dry-run output for native vs. imported
|
|
// "office" spreadsheets. For native tokens parent_type must be "sheet_image";
|
|
// for tokens prefixed with "fake_office_" (the synthetic token an imported
|
|
// office spreadsheet carries) the backend requires "office_sheet_file". The
|
|
// three covered entries — sheets +media-upload (backward), sheets
|
|
// +cells-set-image, and sheets +create-float-image — are every image-upload
|
|
// surface that the office/native split fans out to.
|
|
func TestSheets_ImageUploadDryRunParentType(t *testing.T) {
|
|
setSheetsDryRunEnv(t)
|
|
|
|
workDir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(workDir, "img.png"), []byte("png-bytes"), 0o600))
|
|
|
|
type tc struct {
|
|
name string
|
|
args []string
|
|
token string
|
|
wantParentType string
|
|
}
|
|
tests := []tc{
|
|
{
|
|
name: "media-upload native",
|
|
args: []string{
|
|
"sheets", "+media-upload",
|
|
"--spreadsheet-token", "shtDryRunNative",
|
|
"--file", "img.png",
|
|
"--dry-run",
|
|
},
|
|
token: "shtDryRunNative",
|
|
wantParentType: "sheet_image",
|
|
},
|
|
{
|
|
name: "media-upload office",
|
|
args: []string{
|
|
"sheets", "+media-upload",
|
|
"--spreadsheet-token", "fake_office_dryrun",
|
|
"--file", "img.png",
|
|
"--dry-run",
|
|
},
|
|
token: "fake_office_dryrun",
|
|
wantParentType: "office_sheet_file",
|
|
},
|
|
{
|
|
name: "cells-set-image native",
|
|
args: []string{
|
|
"sheets", "+cells-set-image",
|
|
"--spreadsheet-token", "shtDryRunNative",
|
|
"--sheet-id", "sheet1",
|
|
"--range", "A1",
|
|
"--image", "img.png",
|
|
"--dry-run",
|
|
},
|
|
token: "shtDryRunNative",
|
|
wantParentType: "sheet_image",
|
|
},
|
|
{
|
|
name: "cells-set-image office",
|
|
args: []string{
|
|
"sheets", "+cells-set-image",
|
|
"--spreadsheet-token", "fake_office_dryrun",
|
|
"--sheet-id", "sheet1",
|
|
"--range", "A1",
|
|
"--image", "img.png",
|
|
"--dry-run",
|
|
},
|
|
token: "fake_office_dryrun",
|
|
wantParentType: "office_sheet_file",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
t.Cleanup(cancel)
|
|
|
|
result, err := clie2e.RunCmd(ctx, clie2e.Request{
|
|
Args: tt.args,
|
|
DefaultAs: "user",
|
|
WorkDir: workDir,
|
|
})
|
|
require.NoError(t, err)
|
|
result.AssertExitCode(t, 0)
|
|
|
|
out := result.Stdout
|
|
require.Equal(t, "POST", gjson.Get(out, "api.0.method").String(), "api.0 must be the drive upload; stdout:\n%s", out)
|
|
require.Equal(t, "/open-apis/drive/v1/medias/upload_all",
|
|
gjson.Get(out, "api.0.url").String(), "stdout:\n%s", out)
|
|
require.Equal(t, tt.wantParentType, gjson.Get(out, "api.0.body.parent_type").String(),
|
|
"parent_type for token %q must be %q; stdout:\n%s", tt.token, tt.wantParentType, out)
|
|
require.Equal(t, tt.token, gjson.Get(out, "api.0.body.parent_node").String(),
|
|
"parent_node must equal the spreadsheet token; stdout:\n%s", out)
|
|
})
|
|
}
|
|
}
|