9afb8b7b68
Lark and Feishu are the same product deployed on two isolated clouds (open.larksuite.com and open.feishu.cn). WeKnora touches both from two places — the IM bot and the Wiki data-source connector — and this brings Lark to parity across both. Neither API surface differs, so rather than duplicate either implementation, each is parametrized by a Region and registered twice. IM (internal/im/feishu): Every API call routes through a.api(), which prefixes the region's host, and the WebSocket client passes larkws.WithDomain — the SDK defaults to open.feishu.cn, so Lark long connections would otherwise fail to authenticate. Data source (internal/datasource/connector/feishu): Lark was half-supported: the client honoured a base_url override and a LarkBaseURL constant existed, but the connector registered only as "feishu" and the UI never exposed base_url, so Lark wikis could not be configured at all. Lark is now its own connector type. base_url is kept as an explicit override so data sources that pointed a "feishu" connector at open.larksuite.com keep working. Bugs found and fixed along the way: - Wiki resource links were hardcoded to https://feishu.cn/wiki/{token} in four places, so a Lark wiki linked into the wrong cloud entirely. - Bot identity is now platform-prefixed (feishu:cli_x vs lark:cli_x). BotIdentity carries a unique index, so without this a Lark channel would be rejected as a duplicate of an unrelated Feishu bot sharing an app_id. - The card image cache is now keyed by app_id. image_keys are issued per app on one cloud; a Feishu key handed to a Lark app fails the card update with code=200570. - The datasource doc claimed Lark "adapts automatically" — it could not, since the UI had no way to reach base_url. Permissions are where the clouds genuinely diverge. Identical APIs do not mean identical permission catalogues: aily:file:read, aily:file:write and corehr:file:download exist on Feishu but not on Lark, so pasting Feishu's scope JSON into Lark fails to import. That JSON is also over-broad (it covers products WeKnora never calls) and simultaneously incomplete (it omits the drive:*/docx:* scopes the connector needs). Lark therefore gets its own list, derived from the 17 endpoints the code actually calls and checked identifier-by-identifier against Lark's own API reference, plus a full Feishu→Lark mapping table marking each scope verified, unverified, or absent. Both features use tenant_access_token only — no user_access_ token exists anywhere in the repo — so the user dimension stays empty. Known limitation: Region.ThinkingText makes Lark's initial card placeholder English, but once streaming starts the think-block headers come from the shared MarkdownThinkStyle and are Chinese on every platform (Lark, Slack, Telegram, Mattermost alike). Fixing that means threading a locale through FormatIMDisplayContent, which has no platform context — a cross-platform change, not a Lark one. Documented as such. The channel-creation 400 message is now derived from validIMPlatforms so the list cannot drift as platforms are added. Tests: region invariants and per-region URL construction on both sides, the Feishu/Lark bot-identity split, platform-to-channel mapping, base_url override precedence, an end-to-end SendReply against a fake Open Platform (token fetch, reply, thread-unsupported fallback, hard-error surfacing), and a ListResources drive asserting wiki links never leak the other cloud's host. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
129 lines
4.3 KiB
Go
129 lines
4.3 KiB
Go
package feishu
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/im"
|
|
)
|
|
|
|
// The two regions must stay distinct in every dimension that reaches the
|
|
// network or the session store: sending a Lark request to open.feishu.cn fails
|
|
// authentication, and reporting the wrong platform merges the two clouds'
|
|
// sessions together.
|
|
func TestRegions_AreDistinct(t *testing.T) {
|
|
if RegionFeishu.OpenBaseURL == RegionLark.OpenBaseURL {
|
|
t.Errorf("regions share OpenBaseURL %q", RegionFeishu.OpenBaseURL)
|
|
}
|
|
if RegionFeishu.Platform == RegionLark.Platform {
|
|
t.Errorf("regions share Platform %q", RegionFeishu.Platform)
|
|
}
|
|
if RegionFeishu.Platform != im.PlatformFeishu {
|
|
t.Errorf("RegionFeishu.Platform = %q, want %q", RegionFeishu.Platform, im.PlatformFeishu)
|
|
}
|
|
if RegionLark.Platform != im.PlatformLark {
|
|
t.Errorf("RegionLark.Platform = %q, want %q", RegionLark.Platform, im.PlatformLark)
|
|
}
|
|
}
|
|
|
|
func TestRegions_BaseURLHosts(t *testing.T) {
|
|
cases := []struct {
|
|
region Region
|
|
want string
|
|
}{
|
|
{RegionFeishu, "https://open.feishu.cn"},
|
|
{RegionLark, "https://open.larksuite.com"},
|
|
}
|
|
for _, c := range cases {
|
|
if c.region.OpenBaseURL != c.want {
|
|
t.Errorf("%s OpenBaseURL = %q, want %q", c.region.Label, c.region.OpenBaseURL, c.want)
|
|
}
|
|
if strings.HasSuffix(c.region.OpenBaseURL, "/") {
|
|
t.Errorf("%s OpenBaseURL has a trailing slash: %q", c.region.Label, c.region.OpenBaseURL)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every API call must land on the region's own cloud.
|
|
func TestAdapterAPI_UsesRegionHost(t *testing.T) {
|
|
cases := []struct {
|
|
region Region
|
|
wantPrefix string
|
|
}{
|
|
{RegionFeishu, "https://open.feishu.cn/open-apis/"},
|
|
{RegionLark, "https://open.larksuite.com/open-apis/"},
|
|
}
|
|
for _, c := range cases {
|
|
a := NewAdapter(c.region, "cli_app", "secret", "", "")
|
|
|
|
got := a.api("/open-apis/im/v1/messages/%s/reply", "om_1")
|
|
want := c.wantPrefix + "im/v1/messages/om_1/reply"
|
|
if got != want {
|
|
t.Errorf("%s api() = %q, want %q", c.region.Label, got, want)
|
|
}
|
|
|
|
// A path with no format verbs must survive unchanged.
|
|
if got := a.api("/open-apis/cardkit/v1/cards"); got != c.wantPrefix+"cardkit/v1/cards" {
|
|
t.Errorf("%s api() = %q, want %q", c.region.Label, got, c.wantPrefix+"cardkit/v1/cards")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAdapterPlatform_FollowsRegion(t *testing.T) {
|
|
if got := NewAdapter(RegionFeishu, "a", "b", "", "").Platform(); got != im.PlatformFeishu {
|
|
t.Errorf("Feishu adapter Platform() = %q, want %q", got, im.PlatformFeishu)
|
|
}
|
|
if got := NewAdapter(RegionLark, "a", "b", "", "").Platform(); got != im.PlatformLark {
|
|
t.Errorf("Lark adapter Platform() = %q, want %q", got, im.PlatformLark)
|
|
}
|
|
}
|
|
|
|
// The streaming card placeholder follows the region so Lark users are not shown
|
|
// Chinese copy.
|
|
func TestBuildStreamingCardJSON_PlaceholderFollowsRegion(t *testing.T) {
|
|
for _, region := range []Region{RegionFeishu, RegionLark} {
|
|
raw := buildStreamingCardJSON(region)
|
|
|
|
var card struct {
|
|
Config struct {
|
|
StreamingMode bool `json:"streaming_mode"`
|
|
Summary struct {
|
|
Content string `json:"content"`
|
|
} `json:"summary"`
|
|
} `json:"config"`
|
|
Body struct {
|
|
Elements []struct {
|
|
Content string `json:"content"`
|
|
ElementID string `json:"element_id"`
|
|
} `json:"elements"`
|
|
} `json:"body"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &card); err != nil {
|
|
t.Fatalf("%s card is not valid JSON: %v", region.Label, err)
|
|
}
|
|
|
|
if !card.Config.StreamingMode {
|
|
t.Errorf("%s card has streaming_mode disabled", region.Label)
|
|
}
|
|
if card.Config.Summary.Content != region.ThinkingText {
|
|
t.Errorf("%s summary = %q, want %q", region.Label, card.Config.Summary.Content, region.ThinkingText)
|
|
}
|
|
if len(card.Body.Elements) != 1 {
|
|
t.Fatalf("%s card has %d elements, want 1", region.Label, len(card.Body.Elements))
|
|
}
|
|
el := card.Body.Elements[0]
|
|
if el.ElementID != streamingElementID {
|
|
t.Errorf("%s element_id = %q, want %q", region.Label, el.ElementID, streamingElementID)
|
|
}
|
|
if !strings.Contains(el.Content, region.ThinkingText) {
|
|
t.Errorf("%s element content %q does not contain %q", region.Label, el.Content, region.ThinkingText)
|
|
}
|
|
}
|
|
|
|
// Guard the point of the whole exercise: the copy actually differs.
|
|
if RegionFeishu.ThinkingText == RegionLark.ThinkingText {
|
|
t.Error("Feishu and Lark share ThinkingText; Lark users would see Chinese copy")
|
|
}
|
|
}
|