Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yinzhuang
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xuchentao
yinzhuang
Commits
4281dbde
Commit
4281dbde
authored
Jul 31, 2026
by
xuchentao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: add production CMS import fixture uploader
parent
b22cb9e4
Pipeline
#434
passed with stage
in 21 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
0 deletions
+87
-0
upload-import-fixtures.mjs
scripts/upload-import-fixtures.mjs
+87
-0
No files found.
scripts/upload-import-fixtures.mjs
0 → 100644
View file @
4281dbde
import
assert
from
"node:assert/strict"
;
import
fs
from
"node:fs/promises"
;
import
path
from
"node:path"
;
const
baseUrl
=
process
.
env
.
CMS_BASE_URL
||
"http://101.126.10.129:8791"
;
const
apiKey
=
process
.
env
.
CMS_API_KEY
;
if
(
!
apiKey
)
throw
new
Error
(
"CMS_API_KEY 未配置"
);
const
headers
=
{
Authorization
:
`Bearer
${
apiKey
}
`
};
const
jsonHeaders
=
{
...
headers
,
"Content-Type"
:
"application/json"
};
async
function
request
(
route
,
options
=
{})
{
const
response
=
await
fetch
(
`
${
baseUrl
}
/api/cms
${
route
}
`
,
{
...
options
,
headers
:
{
...
headers
,
...
options
.
headers
},
});
const
payload
=
await
response
.
json
().
catch
(()
=>
({}));
if
(
!
response
.
ok
)
throw
new
Error
(
`
${
options
.
method
||
"GET"
}
${
route
}
返回
${
response
.
status
}
:
${
payload
.
error
||
"未知错误"
}
`
);
return
payload
;
}
const
categories
=
await
request
(
"/categories"
);
for
(
const
required
of
[
"定制灵感"
,
"佩戴养护"
])
assert
.
ok
(
categories
.
categories
.
includes
(
required
));
const
image
=
await
fs
.
readFile
(
"/Users/mac/Desktop/test.png"
);
const
uploadedImage
=
await
request
(
"/uploads"
,
{
method
:
"POST"
,
headers
:
jsonHeaders
,
body
:
JSON
.
stringify
({
dataUrl
:
`data:image/png;base64,
${
image
.
toString
(
"base64"
)}
`
}),
});
const
markdownFile
=
path
.
resolve
(
"tests/fixtures/import-test.md"
);
let
markdown
=
await
fs
.
readFile
(
markdownFile
,
"utf8"
);
const
heading
=
markdown
.
match
(
/^#
\s
+
(
.+
)
$/m
);
assert
.
ok
(
heading
?.
index
!==
undefined
);
const
markdownTitle
=
heading
[
1
].
trim
();
markdown
=
`
${
markdown
.
slice
(
0
,
heading
.
index
)}${
markdown
.
slice
(
heading
.
index
+
heading
[
0
].
length
)}
`
.
trim
()
.
replace
(
"{{TEST_IMAGE_URL}}"
,
uploadedImage
.
url
);
const
markdownImport
=
await
request
(
"/articles"
,
{
method
:
"POST"
,
headers
:
jsonHeaders
,
body
:
JSON
.
stringify
({
title
:
markdownTitle
,
category
:
"定制灵感"
,
body
:
markdown
}),
});
const
docxFile
=
path
.
resolve
(
"tests/fixtures/import-test.docx"
);
const
docx
=
await
fs
.
readFile
(
docxFile
);
const
wordImport
=
await
request
(
"/imports/word"
,
{
method
:
"POST"
,
headers
:
jsonHeaders
,
body
:
JSON
.
stringify
({
fileName
:
path
.
basename
(
docxFile
),
category
:
"佩戴养护"
,
dataUrl
:
`data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,
${
docx
.
toString
(
"base64"
)}
`
,
}),
});
const
markdownArticle
=
await
request
(
`/articles/
${
markdownImport
.
slug
}
`
);
const
wordArticle
=
await
request
(
`/articles/
${
wordImport
.
slug
}
`
);
assert
.
equal
(
markdownImport
.
publishStatus
,
"new-draft"
);
assert
.
equal
(
markdownArticle
.
title
,
markdownTitle
);
assert
.
equal
(
markdownArticle
.
category
,
"定制灵感"
);
assert
.
match
(
markdownArticle
.
body
,
/YINZHUANG-PRODUCTION-IMPORT-20260731-MARKDOWN/
);
assert
.
match
(
markdownArticle
.
body
,
new
RegExp
(
uploadedImage
.
url
.
replace
(
/
[
.*+?^${}()|[
\]\\]
/g
,
"
\\
$&"
)));
assert
.
equal
(
wordImport
.
publishStatus
,
"new-draft"
);
assert
.
equal
(
wordImport
.
title
,
"足银手镯日常佩戴与养护指南"
);
assert
.
equal
(
wordImport
.
imageCount
,
1
);
assert
.
equal
(
wordArticle
.
category
,
"佩戴养护"
);
assert
.
match
(
wordArticle
.
body
,
/YINZHUANG-PRODUCTION-IMPORT-20260731-WORD/
);
assert
.
doesNotMatch
(
wordArticle
.
body
,
/^#
\s
+足银手镯日常佩戴与养护指南/m
);
const
wordImageUrl
=
wordArticle
.
body
.
match
(
/!
\[[^\]]
*
\]\((\/
uploads
\/[^
)
]
+
)\)
/
)?.[
1
];
assert
.
ok
(
wordImageUrl
);
for
(
const
imageUrl
of
[
uploadedImage
.
url
,
wordImageUrl
])
{
const
response
=
await
fetch
(
`
${
baseUrl
}${
imageUrl
}
`
);
assert
.
equal
(
response
.
status
,
200
);
assert
.
equal
(
response
.
headers
.
get
(
"content-type"
),
"image/png"
);
assert
.
deepEqual
(
Buffer
.
from
(
await
response
.
arrayBuffer
()),
image
);
}
console
.
log
(
JSON
.
stringify
({
markdown
:
{
slug
:
markdownImport
.
slug
,
title
:
markdownArticle
.
title
,
imageUrl
:
uploadedImage
.
url
,
publishStatus
:
markdownImport
.
publishStatus
},
word
:
{
slug
:
wordImport
.
slug
,
title
:
wordImport
.
title
,
imageUrl
:
wordImageUrl
,
imageCount
:
wordImport
.
imageCount
,
publishStatus
:
wordImport
.
publishStatus
,
warnings
:
wordImport
.
warnings
},
},
null
,
2
));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment