feat: enhance IM file service tests and refactor storage URL handling
This commit introduces a new test suite for the IM file service, including a stub implementation for testing purposes. It adds tests for resolving file services based on storage providers and ensures proper fallback mechanisms for MinIO URLs. Additionally, the `rewriteStorageURLs` and `cleanIMContent` functions have been refactored to utilize a resolver for improved caching and efficiency. These changes enhance the robustness of file service handling and improve test coverage for various storage scenarios.
This commit is contained in:
@@ -177,7 +177,7 @@ func TestResolveIMFileServiceForPath_LocalSchemeDespiteCOSDefault(t *testing.T)
|
||||
},
|
||||
},
|
||||
}
|
||||
svc := resolveIMFileServiceForPath(tenant, "local://10000/exports/img.png")
|
||||
svc := resolveIMFileServiceForPath(tenant, "local://10000/exports/img.png", nil)
|
||||
require.NotNil(t, svc)
|
||||
got, err := svc.GetFileURL(context.Background(), "local://10000/exports/img.png")
|
||||
require.NoError(t, err)
|
||||
@@ -201,7 +201,7 @@ func TestRewriteStorageURLs_LocalUsesPresignedAPI(t *testing.T) {
|
||||
}
|
||||
|
||||
in := ""
|
||||
out := rewriteStorageURLs(context.Background(), in, tenant)
|
||||
out := rewriteStorageURLs(context.Background(), in, newIMFileServiceResolver(tenant, nil))
|
||||
assert.Contains(t, out, "/api/v1/files/presigned")
|
||||
assert.NotContains(t, out, "myqcloud.com")
|
||||
}
|
||||
@@ -222,11 +222,11 @@ func TestRewriteStorageURLs_COSPathNotSignedAsLocalKey(t *testing.T) {
|
||||
},
|
||||
}
|
||||
path := "cos://test-bucket/ap-shanghai/weknora/10000/exports/abc.png"
|
||||
svc := resolveIMFileServiceForPath(tenant, path)
|
||||
svc := resolveIMFileServiceForPath(tenant, path, nil)
|
||||
require.NotNil(t, svc)
|
||||
|
||||
in := ""
|
||||
out := rewriteStorageURLs(context.Background(), in, tenant)
|
||||
out := rewriteStorageURLs(context.Background(), in, newIMFileServiceResolver(tenant, nil))
|
||||
if out != in {
|
||||
assert.False(t, strings.Contains(out, "local%3A"), "COS URL must not treat local:// as object key")
|
||||
}
|
||||
@@ -245,6 +245,7 @@ func TestFindIncompleteMarkdownImage(t *testing.T) {
|
||||
{"bare provider suffix without markdown", "text minio://wizard-test/10000/exp", -1},
|
||||
{"two images complete", " ", -1},
|
||||
{"first complete second incomplete", "  {
|
||||
@@ -436,11 +437,32 @@ func TestCleanIMContent_AfterStreamReassembly(t *testing.T) {
|
||||
sent, rem := simulateIMStreamFlush(parts)
|
||||
require.Empty(t, rem)
|
||||
joined := strings.Join(sent, "")
|
||||
out := cleanIMContent(context.Background(), joined, tenant)
|
||||
out := cleanIMContent(context.Background(), joined, tenant, nil)
|
||||
assert.Contains(t, out, "/api/v1/files/presigned")
|
||||
assert.NotContains(t, out, "local://10000")
|
||||
}
|
||||
|
||||
func TestHoldbackCutoff_BracketInAlt(t *testing.T) {
|
||||
in := "text ![a[b]](minio://wizard-test/10000/exp"
|
||||
want := strings.Index(in, "![a[b]]")
|
||||
require.GreaterOrEqual(t, want, 0)
|
||||
assert.Equal(t, want, holdbackCutoff(in))
|
||||
}
|
||||
|
||||
func TestSimulateIMStreamFlush_BracketInAltMiddleImage(t *testing.T) {
|
||||
part1 := "### 1\n\n\n### 2\n"
|
||||
part2 := "![a[b]](minio://wizard-test/10000/exports/c91cf852"
|
||||
part3 := "-suffix.png)\n"
|
||||
|
||||
sent, rem := simulateIMStreamFlush([]string{part1, part2, part3})
|
||||
require.Empty(t, rem)
|
||||
joined := strings.Join(sent, "")
|
||||
assert.Contains(t, joined, "![a[b]](minio://wizard-test/10000/exports/c91cf852-suffix.png)")
|
||||
for _, chunk := range sent {
|
||||
assert.False(t, orphanMarkdownImageOpenRe.MatchString(chunk))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteStorageURLs_MultipleImagesInOneChunk(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", "weknora-test-aes-key-32bytes!!!")
|
||||
t.Setenv("APP_EXTERNAL_URL", "https://weknora.example.com")
|
||||
@@ -453,7 +475,7 @@ func TestRewriteStorageURLs_MultipleImagesInOneChunk(t *testing.T) {
|
||||
`` + "\n\n### 3\n\n" +
|
||||
"\n"
|
||||
|
||||
out := rewriteStorageURLs(context.Background(), doc, tenant)
|
||||
out := rewriteStorageURLs(context.Background(), doc, newIMFileServiceResolver(tenant, nil))
|
||||
assert.NotContains(t, out, "local://")
|
||||
assert.Equal(t, 3, strings.Count(out, "/api/v1/files/presigned"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user