Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Rosekey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
freelynetwork
Rosekey
Commits
2205c61e
Commit
2205c61e
authored
2 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
Update utils.ts
parent
02ec5b1d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/backend/test/utils.ts
+34
-38
34 additions, 38 deletions
packages/backend/test/utils.ts
with
34 additions
and
38 deletions
packages/backend/test/utils.ts
+
34
−
38
View file @
2205c61e
import
*
as
fs
from
'
node:fs
'
;
import
{
fileURLToPath
}
from
'
node:url
'
;
import
{
dirname
}
from
'
node:path
'
;
import
*
as
childProcess
from
'
child_process
'
;
import
*
as
http
from
'
node:http
'
;
import
{
SIGKILL
}
from
'
constants
'
;
import
*
as
WebSocket
from
'
ws
'
;
import
*
as
misskey
from
'
misskey-js
'
;
import
fetch
from
'
node-fetch
'
;
import
FormData
from
'
form-data
'
;
import
*
as
childProcess
from
'
child_process
'
;
import
*
as
http
from
'
node:http
'
;
import
{
DataSource
}
from
'
typeorm
'
;
import
loadConfig
from
'
../src/config/load.js
'
;
import
{
SIGKILL
}
from
'
constants
'
;
import
{
entities
}
from
'
../src/db/postgre.js
'
;
const
_filename
=
fileURLToPath
(
import
.
meta
.
url
);
...
...
@@ -27,29 +28,29 @@ export const async = (fn: Function) => (done: Function) => {
export
const
request
=
async
(
endpoint
:
string
,
params
:
any
,
me
?:
any
):
Promise
<
{
body
:
any
,
status
:
number
}
>
=>
{
const
auth
=
me
?
{
i
:
me
.
token
i
:
me
.
token
,
}
:
{};
const
res
=
await
fetch
(
`http://localhost:
${
port
}
/api
${
endpoint
}
`
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
'
Content-Type
'
:
'
application/json
'
,
},
body
:
JSON
.
stringify
(
Object
.
assign
(
auth
,
params
))
body
:
JSON
.
stringify
(
Object
.
assign
(
auth
,
params
))
,
});
const
status
=
res
.
status
;
const
body
=
res
.
status
!==
204
?
await
res
.
json
().
catch
()
:
null
;
return
{
body
,
status
body
,
status
,
};
};
export
const
signup
=
async
(
params
?:
any
):
Promise
<
any
>
=>
{
const
q
=
Object
.
assign
({
username
:
'
test
'
,
password
:
'
test
'
password
:
'
test
'
,
},
params
);
const
res
=
await
request
(
'
/signup
'
,
q
);
...
...
@@ -59,7 +60,7 @@ export const signup = async (params?: any): Promise<any> => {
export
const
post
=
async
(
user
:
any
,
params
?:
misskey
.
Endpoints
[
'
notes/create
'
][
'
req
'
]):
Promise
<
misskey
.
entities
.
Note
>
=>
{
const
q
=
Object
.
assign
({
text
:
'
test
'
text
:
'
test
'
,
},
params
);
const
res
=
await
request
(
'
/notes/create
'
,
q
,
user
);
...
...
@@ -70,26 +71,26 @@ export const post = async (user: any, params?: misskey.Endpoints['notes/create']
export
const
react
=
async
(
user
:
any
,
note
:
any
,
reaction
:
string
):
Promise
<
any
>
=>
{
await
request
(
'
/notes/reactions/create
'
,
{
noteId
:
note
.
id
,
reaction
:
reaction
reaction
:
reaction
,
},
user
);
};
export
const
uploadFile
=
(
user
:
any
,
path
?:
string
):
Promise
<
any
>
=>
{
const
formData
=
new
FormData
();
formData
.
append
(
'
i
'
,
user
.
token
);
formData
.
append
(
'
file
'
,
fs
.
createReadStream
(
path
||
_dirname
+
'
/resources/Lenna.png
'
));
return
fetch
(
`http://localhost:
${
port
}
/api/drive/files/create`
,
{
method
:
'
post
'
,
body
:
formData
,
timeout
:
30
*
1000
,
}).
then
(
res
=>
{
if
(
!
res
.
ok
)
{
throw
`
${
res
.
status
}
${
res
.
statusText
}
`
;
}
else
{
return
res
.
json
();
}
});
const
formData
=
new
FormData
();
formData
.
append
(
'
i
'
,
user
.
token
);
formData
.
append
(
'
file
'
,
fs
.
createReadStream
(
path
||
_dirname
+
'
/resources/Lenna.png
'
));
return
fetch
(
`http://localhost:
${
port
}
/api/drive/files/create`
,
{
method
:
'
post
'
,
body
:
formData
,
timeout
:
30
*
1000
,
}).
then
(
res
=>
{
if
(
!
res
.
ok
)
{
throw
`
${
res
.
status
}
${
res
.
statusText
}
`
;
}
else
{
return
res
.
json
();
}
});
};
export
function
connectStream
(
user
:
any
,
channel
:
string
,
listener
:
(
message
:
Record
<
string
,
any
>
)
=>
any
,
params
?:
any
):
Promise
<
WebSocket
>
{
...
...
@@ -112,8 +113,8 @@ export function connectStream(user: any, channel: string, listener: (message: Re
channel
:
channel
,
id
:
'
a
'
,
pong
:
true
,
params
:
params
}
params
:
params
,
}
,
}));
});
});
...
...
@@ -124,8 +125,8 @@ export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?
return
await
new
Promise
((
resolve
,
reject
)
=>
{
const
req
=
http
.
request
(
`http://localhost:
${
port
}${
path
}
`
,
{
headers
:
{
Accept
:
accept
}
Accept
:
accept
,
}
,
},
res
=>
{
if
(
res
.
statusCode
!
>=
400
)
{
reject
(
res
);
...
...
@@ -146,7 +147,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
return
(
done
:
(
err
?:
Error
)
=>
any
)
=>
{
const
p
=
childProcess
.
spawn
(
'
node
'
,
[
_dirname
+
'
/../index.js
'
],
{
stdio
:
[
'
inherit
'
,
'
inherit
'
,
'
inherit
'
,
'
ipc
'
],
env
:
{
NODE_ENV
:
'
test
'
,
PATH
:
process
.
env
.
PATH
}
env
:
{
NODE_ENV
:
'
test
'
,
PATH
:
process
.
env
.
PATH
}
,
});
callbackSpawnedProcess
(
p
);
p
.
on
(
'
message
'
,
message
=>
{
...
...
@@ -158,12 +159,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
export
async
function
initTestDb
(
justBorrow
=
false
,
initEntities
?:
any
[])
{
if
(
process
.
env
.
NODE_ENV
!==
'
test
'
)
throw
'
NODE_ENV is not a test
'
;
try
{
const
conn
=
await
getConnection
();
await
conn
.
close
();
}
catch
(
e
)
{}
return
await
createConnection
({
return
new
DataSource
({
type
:
'
postgres
'
,
host
:
config
.
db
.
host
,
port
:
config
.
db
.
port
,
...
...
@@ -172,7 +168,7 @@ export async function initTestDb(justBorrow = false, initEntities?: any[]) {
database
:
config
.
db
.
db
,
synchronize
:
true
&&
!
justBorrow
,
dropSchema
:
true
&&
!
justBorrow
,
entities
:
initEntities
||
entities
entities
:
initEntities
||
entities
,
});
}
...
...
@@ -185,7 +181,7 @@ export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProc
const
p
=
childProcess
.
spawn
(
'
node
'
,
[
_dirname
+
'
/../built/index.js
'
],
{
stdio
:
[
'
inherit
'
,
'
inherit
'
,
'
inherit
'
,
'
ipc
'
],
env
:
{
NODE_ENV
:
'
test
'
,
PATH
:
process
.
env
.
PATH
}
env
:
{
NODE_ENV
:
'
test
'
,
PATH
:
process
.
env
.
PATH
}
,
});
p
.
on
(
'
error
'
,
e
=>
rej
(
e
));
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment