@aebzx/slang-tool 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/command.ts +6 -2
- package/dist/cli.js +25 -20
- package/package.json +2 -2
- package/test/net.test.ts +107 -0
- package/utils/net.ts +14 -11
package/command.ts
CHANGED
|
@@ -210,8 +210,12 @@ async function config_verify(){
|
|
|
210
210
|
}
|
|
211
211
|
async function register(username:string,email:string){
|
|
212
212
|
let config=global_config()
|
|
213
|
-
|
|
214
|
-
|
|
213
|
+
//服务端 register 在响应中返回 token(即使 SMTP 不可用也能拿到),自动写入全局配置
|
|
214
|
+
let res=await net.user.register(username,email)
|
|
215
|
+
config.username=username
|
|
216
|
+
config.token=res.token
|
|
217
|
+
write_global_config(config)
|
|
218
|
+
console.log('注册成功,token已配置到全局配置,可直接 publish')
|
|
215
219
|
}
|
|
216
220
|
function compiler(){
|
|
217
221
|
//读取虚拟环境运行:编译器是 init 时下载到 venv/compiler.js 的 JS 文件,用 node 执行
|
package/dist/cli.js
CHANGED
|
@@ -15400,65 +15400,67 @@ async function download_module(name, version) {
|
|
|
15400
15400
|
})).data), "base64");
|
|
15401
15401
|
}
|
|
15402
15402
|
async function download_compiler(large_version, small_version) {
|
|
15403
|
-
return check(await ajax.post("/api/download/compiler", {
|
|
15403
|
+
return check((await ajax.post("/api/download/compiler", {
|
|
15404
15404
|
large_version,
|
|
15405
15405
|
small_version
|
|
15406
|
-
}));
|
|
15406
|
+
})).data);
|
|
15407
15407
|
}
|
|
15408
15408
|
async function list_vm() {
|
|
15409
|
-
return check(await ajax.get("/api/list/vm"));
|
|
15409
|
+
return check((await ajax.get("/api/list/vm")).data);
|
|
15410
15410
|
}
|
|
15411
15411
|
async function list_module() {
|
|
15412
|
-
return check(await ajax.get("/api/list/module"));
|
|
15412
|
+
return check((await ajax.get("/api/list/module")).data);
|
|
15413
15413
|
}
|
|
15414
15414
|
async function list_compiler() {
|
|
15415
|
-
return check(await ajax.get("/api/list/compiler"));
|
|
15415
|
+
return check((await ajax.get("/api/list/compiler")).data);
|
|
15416
15416
|
}
|
|
15417
15417
|
async function publish_vm$1(author, token, module, data) {
|
|
15418
|
-
return check(await ajax.post("/api/publish/vm", {
|
|
15418
|
+
return check((await ajax.post("/api/publish/vm", {
|
|
15419
15419
|
author,
|
|
15420
15420
|
token,
|
|
15421
15421
|
module,
|
|
15422
15422
|
data
|
|
15423
|
-
}));
|
|
15423
|
+
})).data);
|
|
15424
15424
|
}
|
|
15425
15425
|
async function publish_module$1(author, token, module, data) {
|
|
15426
|
-
return check(await ajax.post("/api/publish/module", {
|
|
15426
|
+
return check((await ajax.post("/api/publish/module", {
|
|
15427
15427
|
author,
|
|
15428
15428
|
token,
|
|
15429
15429
|
module,
|
|
15430
15430
|
data
|
|
15431
|
-
}));
|
|
15431
|
+
})).data);
|
|
15432
15432
|
}
|
|
15433
15433
|
async function publish_compiler$1(author, token, version, type, data) {
|
|
15434
|
-
return check(await ajax.post("/api/publish/compiler/add", {
|
|
15434
|
+
return check((await ajax.post("/api/publish/compiler/add", {
|
|
15435
15435
|
author,
|
|
15436
15436
|
token,
|
|
15437
15437
|
version,
|
|
15438
15438
|
type,
|
|
15439
15439
|
data
|
|
15440
|
-
}));
|
|
15440
|
+
})).data);
|
|
15441
15441
|
}
|
|
15442
15442
|
async function create_compiler$1(author, token, license, version) {
|
|
15443
|
-
return check(await ajax.post("/api/publish/compiler/create", {
|
|
15443
|
+
return check((await ajax.post("/api/publish/compiler/create", {
|
|
15444
15444
|
author,
|
|
15445
15445
|
token,
|
|
15446
15446
|
license,
|
|
15447
15447
|
version
|
|
15448
|
-
}));
|
|
15448
|
+
})).data);
|
|
15449
15449
|
}
|
|
15450
15450
|
async function register$1(username, email) {
|
|
15451
|
-
return check(await ajax.post("/api/register", {
|
|
15451
|
+
return check((await ajax.post("/api/register", {
|
|
15452
15452
|
username,
|
|
15453
15453
|
email
|
|
15454
|
-
}));
|
|
15454
|
+
})).data);
|
|
15455
15455
|
}
|
|
15456
15456
|
async function verify(username, token) {
|
|
15457
15457
|
if ((await ajax.get("/api/health")).data.message != "is SPM Server") return "不是SPM服务器";
|
|
15458
|
-
|
|
15458
|
+
let res = (await ajax.post("/api/verify", {
|
|
15459
15459
|
username,
|
|
15460
15460
|
token
|
|
15461
|
-
}))
|
|
15461
|
+
})).data;
|
|
15462
|
+
if (res.code != 200 || !res.data) return "用户不存在或者token配置不正确";
|
|
15463
|
+
return "";
|
|
15462
15464
|
}
|
|
15463
15465
|
var net_default = {
|
|
15464
15466
|
download: {
|
|
@@ -15693,9 +15695,12 @@ async function config_verify() {
|
|
|
15693
15695
|
if (data != "") throw new Error(data);
|
|
15694
15696
|
}
|
|
15695
15697
|
async function register(username, email) {
|
|
15696
|
-
global_config();
|
|
15697
|
-
await net_default.user.register(username, email);
|
|
15698
|
-
|
|
15698
|
+
let config = global_config();
|
|
15699
|
+
let res = await net_default.user.register(username, email);
|
|
15700
|
+
config.username = username;
|
|
15701
|
+
config.token = res.token;
|
|
15702
|
+
write_global_config(config);
|
|
15703
|
+
console.log("注册成功,token已配置到全局配置,可直接 publish");
|
|
15699
15704
|
}
|
|
15700
15705
|
function compiler() {
|
|
15701
15706
|
let config = project_config(process.cwd());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aebzx/slang-tool",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "vitest run",
|
|
11
|
-
"
|
|
11
|
+
"pub": "npm publish"
|
|
12
12
|
},
|
|
13
13
|
"bin": {
|
|
14
14
|
"slang": "dist/cli.js"
|
package/test/net.test.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//cli net 层单元测试:验证 check() 正确处理 axios 响应
|
|
2
|
+
//关键点:ajax 返回 axios 响应 {status,data:Result},check 应接收 Result 而非整个响应
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
vi.mock('../utils/ajax.ts', () => ({
|
|
6
|
+
default: {
|
|
7
|
+
get: vi.fn(),
|
|
8
|
+
post: vi.fn()
|
|
9
|
+
}
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
import net from '../utils/net.ts'
|
|
13
|
+
import ajax from '../utils/ajax.ts'
|
|
14
|
+
|
|
15
|
+
const mockPost = ajax.post as ReturnType<typeof vi.fn>
|
|
16
|
+
const mockGet = ajax.get as ReturnType<typeof vi.fn>
|
|
17
|
+
|
|
18
|
+
//axios 响应形状:Result 在 .data 里
|
|
19
|
+
const axiosRes = (result: any) => ({ data: result, status: result.code, statusText: '', headers: {}, config: {} })
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.clearAllMocks()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe('register', () => {
|
|
26
|
+
it('服务端返回 200 时不抛错并返回 token', async () => {
|
|
27
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Registration successful', data: { token: 'abc123' }, code: 200 }))
|
|
28
|
+
const res = await net.user.register('alice', 'alice@spm.dev')
|
|
29
|
+
expect(res).toEqual({ token: 'abc123' })
|
|
30
|
+
expect(mockPost).toHaveBeenCalledWith('/api/register', { username: 'alice', email: 'alice@spm.dev' })
|
|
31
|
+
})
|
|
32
|
+
it('服务端返回 400 时抛出服务端 message', async () => {
|
|
33
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'User already exists', data: null, code: 400 }))
|
|
34
|
+
await expect(net.user.register('alice', 'alice@spm.dev')).rejects.toThrow('User already exists')
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('verify', () => {
|
|
39
|
+
it('校验通过返回空字符串', async () => {
|
|
40
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'is SPM Server', data: null, code: 200 }))
|
|
41
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Verification successful', data: true, code: 200 }))
|
|
42
|
+
await expect(net.user.verify('alice', 'tok')).resolves.toBe('')
|
|
43
|
+
})
|
|
44
|
+
it('校验失败返回错误提示', async () => {
|
|
45
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'is SPM Server', data: null, code: 200 }))
|
|
46
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Invalid username or token', data: false, code: 400 }))
|
|
47
|
+
await expect(net.user.verify('alice', 'bad')).resolves.toBe('用户不存在或者token配置不正确')
|
|
48
|
+
})
|
|
49
|
+
it('非 SPM 服务器时提示', async () => {
|
|
50
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'hello', data: null, code: 200 }))
|
|
51
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Verification successful', data: true, code: 200 }))
|
|
52
|
+
await expect(net.user.verify('alice', 'tok')).resolves.toBe('不是SPM服务器')
|
|
53
|
+
//health 检查失败后不应再调用 verify 端点
|
|
54
|
+
expect(mockPost).not.toHaveBeenCalled()
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('list', () => {
|
|
59
|
+
it('list/vm 返回数组', async () => {
|
|
60
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'Success', data: [], code: 200 }))
|
|
61
|
+
await expect(net.list.vm()).resolves.toEqual([])
|
|
62
|
+
})
|
|
63
|
+
it('list/module 返回数组', async () => {
|
|
64
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'Success', data: [], code: 200 }))
|
|
65
|
+
await expect(net.list.module()).resolves.toEqual([])
|
|
66
|
+
})
|
|
67
|
+
it('list/compiler 返回数组', async () => {
|
|
68
|
+
mockGet.mockResolvedValue(axiosRes({ message: 'Success', data: [], code: 200 }))
|
|
69
|
+
await expect(net.list.compiler()).resolves.toEqual([])
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('publish', () => {
|
|
74
|
+
it('publish/module 成功', async () => {
|
|
75
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: null, code: 200 }))
|
|
76
|
+
await expect(net.publish.module('alice', 'tok', {
|
|
77
|
+
version: '1.0.0', dependencies: [], source: '', hex: 'abc'
|
|
78
|
+
}, Buffer.from('x'))).resolves.toBeNull()
|
|
79
|
+
})
|
|
80
|
+
it('publish/vm 成功', async () => {
|
|
81
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: null, code: 200 }))
|
|
82
|
+
await expect(net.publish.vm('alice', 'tok', {
|
|
83
|
+
version: '1.0', isa: 'x64', author: 'alice', license: 'MIT', source: '', hex: 'abc'
|
|
84
|
+
}, Buffer.from('x'))).resolves.toBeNull()
|
|
85
|
+
})
|
|
86
|
+
it('publish/compiler 成功', async () => {
|
|
87
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: null, code: 200 }))
|
|
88
|
+
await expect(net.publish.compiler('alice', 'tok', '1.0', {
|
|
89
|
+
version: '1.0.1', hex: 'abc', source: ''
|
|
90
|
+
}, 'src')).resolves.toBeNull()
|
|
91
|
+
})
|
|
92
|
+
it('create/compiler 成功', async () => {
|
|
93
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: null, code: 200 }))
|
|
94
|
+
await expect(net.publish.compiler_create('alice', 'tok', 'MIT', '1.0')).resolves.toBeNull()
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('download', () => {
|
|
99
|
+
it('download/compiler 返回字符串', async () => {
|
|
100
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: 'compiler-js', code: 200 }))
|
|
101
|
+
await expect(net.download.compiler('1.0', '1.0.1')).resolves.toBe('compiler-js')
|
|
102
|
+
})
|
|
103
|
+
it('download/vm 返回 Buffer', async () => {
|
|
104
|
+
mockPost.mockResolvedValue(axiosRes({ message: 'Success', data: Buffer.from('bin').toString('base64'), code: 200 }))
|
|
105
|
+
await expect(net.download.vm('1.0')).resolves.toEqual(Buffer.from('bin'))
|
|
106
|
+
})
|
|
107
|
+
})
|
package/utils/net.ts
CHANGED
|
@@ -21,35 +21,38 @@ async function download_module(name:string,version:string){
|
|
|
21
21
|
return Buffer.from(check((await ajax.post('/api/download/module',{name,version})).data) as string,'base64')
|
|
22
22
|
}
|
|
23
23
|
async function download_compiler(large_version:string,small_version:string){
|
|
24
|
-
return check<string>(await ajax.post('/api/download/compiler',{large_version,small_version}))
|
|
24
|
+
return check<string>((await ajax.post('/api/download/compiler',{large_version,small_version})).data)
|
|
25
25
|
}
|
|
26
26
|
async function list_vm(){
|
|
27
|
-
return check<VMConfig>(await ajax.get('/api/list/vm'))
|
|
27
|
+
return check<VMConfig>((await ajax.get('/api/list/vm')).data)
|
|
28
28
|
}
|
|
29
29
|
async function list_module(){
|
|
30
|
-
return check<ModuleConfig>(await ajax.get('/api/list/module'))
|
|
30
|
+
return check<ModuleConfig>((await ajax.get('/api/list/module')).data)
|
|
31
31
|
}
|
|
32
32
|
async function list_compiler(){
|
|
33
|
-
return check<CompilerConfig>(await ajax.get('/api/list/compiler'))
|
|
33
|
+
return check<CompilerConfig>((await ajax.get('/api/list/compiler')).data)
|
|
34
34
|
}
|
|
35
35
|
async function publish_vm(author:string,token:string,module:VM,data:Buffer){
|
|
36
|
-
return check<void>(await ajax.post('/api/publish/vm',{author,token,module,data}))
|
|
36
|
+
return check<void>((await ajax.post('/api/publish/vm',{author,token,module,data})).data)
|
|
37
37
|
}
|
|
38
38
|
async function publish_module(author:string,token:string,module:ModuleVersion,data:Buffer){
|
|
39
|
-
return check<void>(await ajax.post('/api/publish/module',{author,token,module,data}))
|
|
39
|
+
return check<void>((await ajax.post('/api/publish/module',{author,token,module,data})).data)
|
|
40
40
|
}
|
|
41
41
|
async function publish_compiler(author:string,token:string,version:string,type:CompilerChild,data:string){
|
|
42
|
-
return check<void>(await ajax.post('/api/publish/compiler/add',{author,token,version,type,data}))
|
|
42
|
+
return check<void>((await ajax.post('/api/publish/compiler/add',{author,token,version,type,data})).data)
|
|
43
43
|
}
|
|
44
44
|
async function create_compiler(author:string,token:string,license:string,version:string){
|
|
45
|
-
return check<void>(await ajax.post('/api/publish/compiler/create',{author,token,license,version}))
|
|
45
|
+
return check<void>((await ajax.post('/api/publish/compiler/create',{author,token,license,version})).data)
|
|
46
46
|
}
|
|
47
47
|
async function register(username:string,email:string){
|
|
48
|
-
return check<
|
|
48
|
+
return check<{token:string}>((await ajax.post('/api/register',{username,email})).data)
|
|
49
49
|
}
|
|
50
50
|
async function verify(username:string,token:string){
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
let health=(await ajax.get('/api/health')).data as Result<void>
|
|
52
|
+
if(health.message!='is SPM Server')return '不是SPM服务器'
|
|
53
|
+
let res=(await ajax.post('/api/verify',{username,token})).data as Result<boolean>
|
|
54
|
+
if(res.code!=200||!res.data)return '用户不存在或者token配置不正确'
|
|
55
|
+
return ''
|
|
53
56
|
}
|
|
54
57
|
export default {
|
|
55
58
|
download:{
|