@tscircuit/fake-snippets 0.0.8 → 0.0.10
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/bun-tests/fake-snippets-api/routes/packages/list-1.test.ts +0 -3
- package/bun-tests/fake-snippets-api/routes/packages/update.test.ts +161 -0
- package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +32 -27
- package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +114 -0
- package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +10 -6
- package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +8 -6
- package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +2 -2
- package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +10 -10
- package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +8 -6
- package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +1 -1
- package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +19 -12
- package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +57 -16
- package/bun.lock +145 -569
- package/dist/bundle.js +905 -248
- package/fake-snippets-api/lib/db/db-client.ts +748 -108
- package/fake-snippets-api/lib/db/schema.ts +12 -0
- package/fake-snippets-api/lib/public-mapping/public-map-package.ts +2 -1
- package/fake-snippets-api/routes/api/packages/list.ts +4 -1
- package/fake-snippets-api/routes/api/packages/update.ts +91 -0
- package/fake-snippets-api/routes/api/snippets/add_star.ts +30 -8
- package/fake-snippets-api/routes/api/snippets/create.ts +16 -16
- package/fake-snippets-api/routes/api/snippets/delete.ts +5 -5
- package/fake-snippets-api/routes/api/snippets/download.ts +24 -10
- package/fake-snippets-api/routes/api/snippets/get.ts +46 -13
- package/fake-snippets-api/routes/api/snippets/list.ts +37 -2
- package/fake-snippets-api/routes/api/snippets/update.ts +36 -14
- package/package.json +3 -5
- package/src/components/CodeAndPreview.tsx +13 -48
- package/src/components/CodeEditor.tsx +10 -7
- package/src/components/EditorNav.tsx +0 -21
- package/src/components/PreviewContent.tsx +2 -2
- package/src/components/ViewSnippetHeader.tsx +4 -0
- package/src/hooks/use-global-store.ts +0 -5
- package/src/hooks/use-package-as-snippet.ts +78 -0
- package/src/hooks/use-run-tsx/index.tsx +4 -0
- package/src/lib/jlc-parts-engine.ts +4 -2
|
@@ -4,9 +4,6 @@ import { expect, test } from "bun:test"
|
|
|
4
4
|
test("list packages", async () => {
|
|
5
5
|
const { axios, db } = await getTestServer()
|
|
6
6
|
|
|
7
|
-
// Log the initial state of packages in the database
|
|
8
|
-
console.log("Initial packages in DB:", db.packages)
|
|
9
|
-
|
|
10
7
|
// Add some test packages
|
|
11
8
|
const packages = [
|
|
12
9
|
{
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
+
import { expect, test } from "bun:test"
|
|
3
|
+
|
|
4
|
+
test("update package", async () => {
|
|
5
|
+
const { axios, db } = await getTestServer()
|
|
6
|
+
|
|
7
|
+
// First create a package
|
|
8
|
+
const packageResponse = await axios.post(
|
|
9
|
+
"/api/packages/create",
|
|
10
|
+
{
|
|
11
|
+
name: "test-package",
|
|
12
|
+
description: "Test Description",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
headers: {
|
|
16
|
+
Authorization: "Bearer 1234",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
)
|
|
20
|
+
const packageId = packageResponse.data.package.package_id
|
|
21
|
+
|
|
22
|
+
// Update the package
|
|
23
|
+
const response = await axios.post(
|
|
24
|
+
"/api/packages/update",
|
|
25
|
+
{
|
|
26
|
+
package_id: packageId,
|
|
27
|
+
name: "updated-package",
|
|
28
|
+
description: "Updated Description",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
headers: {
|
|
32
|
+
Authorization: "Bearer 1234",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
expect(response.status).toBe(200)
|
|
38
|
+
expect(response.data.ok).toBe(true)
|
|
39
|
+
expect(response.data.package.name).toBe("testuser/updated-package")
|
|
40
|
+
expect(response.data.package.description).toBe("Updated Description")
|
|
41
|
+
|
|
42
|
+
// Verify the package was updated in the database
|
|
43
|
+
const updatedPackage = db.packages.find((p) => p.package_id === packageId)
|
|
44
|
+
expect(updatedPackage?.name).toBe("testuser/updated-package")
|
|
45
|
+
expect(updatedPackage?.description).toBe("Updated Description")
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test("update package privacy settings", async () => {
|
|
49
|
+
const { axios, db } = await getTestServer()
|
|
50
|
+
|
|
51
|
+
// Create initial public package
|
|
52
|
+
const packageResponse = await axios.post(
|
|
53
|
+
"/api/packages/create",
|
|
54
|
+
{
|
|
55
|
+
name: "public-package",
|
|
56
|
+
description: "Public Package",
|
|
57
|
+
is_private: false,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
headers: {
|
|
61
|
+
Authorization: "Bearer 1234",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
)
|
|
65
|
+
const packageId = packageResponse.data.package.package_id
|
|
66
|
+
|
|
67
|
+
// Update to make it private
|
|
68
|
+
const response = await axios.post(
|
|
69
|
+
"/api/packages/update",
|
|
70
|
+
{
|
|
71
|
+
package_id: packageId,
|
|
72
|
+
is_private: true,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
headers: {
|
|
76
|
+
Authorization: "Bearer 1234",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
expect(response.status).toBe(200)
|
|
82
|
+
expect(response.data.ok).toBe(true)
|
|
83
|
+
expect(response.data.package.is_private).toBe(true)
|
|
84
|
+
expect(response.data.package.is_public).toBe(false)
|
|
85
|
+
expect(response.data.package.is_unlisted).toBe(true) // Private packages should be unlisted
|
|
86
|
+
|
|
87
|
+
// Verify in database
|
|
88
|
+
const updatedPackage = db.packages.find((p) => p.package_id === packageId)
|
|
89
|
+
expect(updatedPackage?.is_private).toBe(true)
|
|
90
|
+
expect(updatedPackage?.is_public).toBe(false)
|
|
91
|
+
expect(updatedPackage?.is_unlisted).toBe(true)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test("update non-existent package", async () => {
|
|
95
|
+
const { axios } = await getTestServer()
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
await axios.post(
|
|
99
|
+
"/api/packages/update",
|
|
100
|
+
{
|
|
101
|
+
package_id: "non-existent-id",
|
|
102
|
+
name: "updated-name",
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
headers: {
|
|
106
|
+
Authorization: "Bearer 1234",
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
)
|
|
110
|
+
throw new Error("Expected request to fail")
|
|
111
|
+
} catch (error: any) {
|
|
112
|
+
expect(error.status).toBe(404)
|
|
113
|
+
expect(error.data.error.error_code).toBe("package_not_found")
|
|
114
|
+
expect(error.data.error.message).toBe("Package not found")
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test("update package without permission", async () => {
|
|
119
|
+
const { axios, db } = await getTestServer()
|
|
120
|
+
|
|
121
|
+
db.addPackage({
|
|
122
|
+
name: "Package3",
|
|
123
|
+
unscoped_name: "Package3",
|
|
124
|
+
owner_github_username: "user1",
|
|
125
|
+
creator_account_id: "creator1",
|
|
126
|
+
created_at: "2023-01-03T00:00:00Z",
|
|
127
|
+
updated_at: "2023-01-03T00:00:00Z",
|
|
128
|
+
description: "Description 3",
|
|
129
|
+
ai_description: "AI Description 3",
|
|
130
|
+
owner_org_id: "org1",
|
|
131
|
+
latest_version: "1.0.0",
|
|
132
|
+
license: "MIT",
|
|
133
|
+
is_source_from_github: true,
|
|
134
|
+
star_count: 0,
|
|
135
|
+
} as any)
|
|
136
|
+
|
|
137
|
+
const packageId = db.packages[0].package_id
|
|
138
|
+
|
|
139
|
+
// Try to update with different user
|
|
140
|
+
try {
|
|
141
|
+
await axios.post(
|
|
142
|
+
"/api/packages/update",
|
|
143
|
+
{
|
|
144
|
+
package_id: packageId,
|
|
145
|
+
name: "stolen-package",
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
headers: {
|
|
149
|
+
Authorization: "Bearer 5678", // Different user
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
)
|
|
153
|
+
throw new Error("Expected request to fail")
|
|
154
|
+
} catch (error: any) {
|
|
155
|
+
expect(error.status).toBe(403)
|
|
156
|
+
expect(error.data.error.error_code).toBe("forbidden")
|
|
157
|
+
expect(error.data.error.message).toBe(
|
|
158
|
+
"You don't have permission to update this package",
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
})
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import {
|
|
2
|
+
import { expect, test } from "bun:test"
|
|
3
3
|
|
|
4
4
|
test("add star to snippet", async () => {
|
|
5
|
-
const { axios
|
|
5
|
+
const { axios } = await getTestServer()
|
|
6
6
|
|
|
7
|
-
//
|
|
8
|
-
const
|
|
9
|
-
unscoped_name: "TestSnippet",
|
|
10
|
-
owner_name: "otheruser",
|
|
7
|
+
// Create a test snippet using the create endpoint
|
|
8
|
+
const newSnippetData = {
|
|
11
9
|
code: "Test Content",
|
|
12
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
13
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
name: "otheruser/TestSnippet",
|
|
15
10
|
snippet_type: "package",
|
|
16
11
|
description: "Test Description",
|
|
17
12
|
}
|
|
18
|
-
const
|
|
13
|
+
const createResponse = await axios.post(
|
|
14
|
+
"/api/snippets/create",
|
|
15
|
+
newSnippetData,
|
|
16
|
+
)
|
|
17
|
+
expect(createResponse.status).toBe(200)
|
|
18
|
+
const createdSnippet = createResponse.data.snippet
|
|
19
19
|
|
|
20
20
|
// Star the snippet
|
|
21
21
|
const response = await axios.post(
|
|
22
22
|
"/api/snippets/add_star",
|
|
23
23
|
{
|
|
24
|
-
snippet_id:
|
|
24
|
+
snippet_id: createdSnippet.snippet_id,
|
|
25
25
|
},
|
|
26
26
|
{
|
|
27
27
|
headers: {
|
|
@@ -32,12 +32,17 @@ test("add star to snippet", async () => {
|
|
|
32
32
|
|
|
33
33
|
expect(response.status).toBe(200)
|
|
34
34
|
expect(response.data.ok).toBe(true)
|
|
35
|
-
expect(response.data.account_snippet).toBeDefined()
|
|
36
|
-
expect(response.data.account_snippet.snippet_id).toBe(addedSnippet.snippet_id)
|
|
37
|
-
expect(response.data.account_snippet.has_starred).toBe(true)
|
|
38
35
|
|
|
39
|
-
// Verify star was added
|
|
40
|
-
|
|
36
|
+
// Verify star was added by checking the snippet again
|
|
37
|
+
const getResponse = await axios.get("/api/snippets/get", {
|
|
38
|
+
params: { snippet_id: createdSnippet.snippet_id },
|
|
39
|
+
headers: {
|
|
40
|
+
Authorization: "Bearer 1234",
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
expect(getResponse.status).toBe(200)
|
|
45
|
+
expect(getResponse.data.snippet.is_starred).toBe(true)
|
|
41
46
|
})
|
|
42
47
|
|
|
43
48
|
test("add star to non-existent snippet", async () => {
|
|
@@ -63,26 +68,26 @@ test("add star to non-existent snippet", async () => {
|
|
|
63
68
|
})
|
|
64
69
|
|
|
65
70
|
test("add star to already starred snippet", async () => {
|
|
66
|
-
const { axios
|
|
71
|
+
const { axios } = await getTestServer()
|
|
67
72
|
|
|
68
|
-
//
|
|
69
|
-
const
|
|
70
|
-
unscoped_name: "TestSnippet",
|
|
71
|
-
owner_name: "otheruser",
|
|
73
|
+
// Create a test snippet using the create endpoint
|
|
74
|
+
const newSnippetData = {
|
|
72
75
|
code: "Test Content",
|
|
73
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
74
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
75
|
-
name: "otheruser/TestSnippet",
|
|
76
76
|
snippet_type: "package",
|
|
77
77
|
description: "Test Description",
|
|
78
78
|
}
|
|
79
|
-
const
|
|
79
|
+
const createResponse = await axios.post(
|
|
80
|
+
"/api/snippets/create",
|
|
81
|
+
newSnippetData,
|
|
82
|
+
)
|
|
83
|
+
expect(createResponse.status).toBe(200)
|
|
84
|
+
const createdSnippet = createResponse.data.snippet
|
|
80
85
|
|
|
81
86
|
// Star the snippet first time
|
|
82
87
|
await axios.post(
|
|
83
88
|
"/api/snippets/add_star",
|
|
84
89
|
{
|
|
85
|
-
snippet_id:
|
|
90
|
+
snippet_id: createdSnippet.snippet_id,
|
|
86
91
|
},
|
|
87
92
|
{
|
|
88
93
|
headers: {
|
|
@@ -96,7 +101,7 @@ test("add star to already starred snippet", async () => {
|
|
|
96
101
|
await axios.post(
|
|
97
102
|
"/api/snippets/add_star",
|
|
98
103
|
{
|
|
99
|
-
snippet_id:
|
|
104
|
+
snippet_id: createdSnippet.snippet_id,
|
|
100
105
|
},
|
|
101
106
|
{
|
|
102
107
|
headers: {
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
+
import { expect, test } from "bun:test"
|
|
3
|
+
import { snippetSchema } from "fake-snippets-api/lib/db/schema"
|
|
4
|
+
|
|
5
|
+
test("GET /api/snippets/get - should return snippet by snippet_id", async () => {
|
|
6
|
+
const { axios } = await getTestServer()
|
|
7
|
+
|
|
8
|
+
// First create a snippet
|
|
9
|
+
const newSnippetData = {
|
|
10
|
+
code: "console.log('Hello World')",
|
|
11
|
+
snippet_type: "package",
|
|
12
|
+
description: "A test snippet",
|
|
13
|
+
compiled_js: "console.log('Hello World')",
|
|
14
|
+
dts: "export function hello(): void;",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const createResponse = await axios.post(
|
|
18
|
+
"/api/snippets/create",
|
|
19
|
+
newSnippetData,
|
|
20
|
+
)
|
|
21
|
+
expect(createResponse.status).toBe(200)
|
|
22
|
+
const createdSnippet = createResponse.data.snippet
|
|
23
|
+
|
|
24
|
+
// Get the created snippet using the /get endpoint
|
|
25
|
+
const getResponse = await axios.get("/api/snippets/get", {
|
|
26
|
+
params: { snippet_id: createdSnippet.snippet_id },
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
expect(getResponse.status).toBe(200)
|
|
30
|
+
const responseBody = getResponse.data
|
|
31
|
+
expect(responseBody.ok).toBe(true)
|
|
32
|
+
expect(responseBody.snippet).toEqual(snippetSchema.parse(createdSnippet))
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test("GET /api/snippets/get - should return 404 if snippet not found", async () => {
|
|
36
|
+
const { axios } = await getTestServer()
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
await axios.get("/api/snippets/get", {
|
|
40
|
+
params: { snippet_id: "non_existent_snippet_id" },
|
|
41
|
+
})
|
|
42
|
+
throw new Error("Expected request to fail")
|
|
43
|
+
} catch (error: any) {
|
|
44
|
+
expect(error.status).toBe(404)
|
|
45
|
+
expect(error.data.error.error_code).toBe("snippet_not_found")
|
|
46
|
+
expect(error.data.error.message).toBe(
|
|
47
|
+
'Snippet not found (searched using {"snippet_id":"non_existent_snippet_id"})',
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test("GET /api/snippets/get - should return snippet by name and owner", async () => {
|
|
53
|
+
const { axios } = await getTestServer()
|
|
54
|
+
|
|
55
|
+
// First create a snippet
|
|
56
|
+
const newSnippetData = {
|
|
57
|
+
code: "console.log('Hello World')",
|
|
58
|
+
snippet_type: "package",
|
|
59
|
+
description: "A test snippet",
|
|
60
|
+
compiled_js: "console.log('Hello World')",
|
|
61
|
+
dts: "export function hello(): void;",
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const createResponse = await axios.post(
|
|
65
|
+
"/api/snippets/create",
|
|
66
|
+
newSnippetData,
|
|
67
|
+
)
|
|
68
|
+
expect(createResponse.status).toBe(200)
|
|
69
|
+
const createdSnippet = createResponse.data.snippet
|
|
70
|
+
|
|
71
|
+
// Get the snippet using name and owner
|
|
72
|
+
const getResponse = await axios.get("/api/snippets/get", {
|
|
73
|
+
params: {
|
|
74
|
+
name: createdSnippet.name,
|
|
75
|
+
owner_name: createdSnippet.owner_name,
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
expect(getResponse.status).toBe(200)
|
|
80
|
+
const responseBody = getResponse.data
|
|
81
|
+
expect(responseBody.ok).toBe(true)
|
|
82
|
+
expect(responseBody.snippet).toEqual(snippetSchema.parse(createdSnippet))
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test("GET /api/snippets/get - should return snippet by unscoped_name and owner", async () => {
|
|
86
|
+
const { axios, db } = await getTestServer()
|
|
87
|
+
|
|
88
|
+
// First create a snippet
|
|
89
|
+
const snippet = {
|
|
90
|
+
unscoped_name: "test-package",
|
|
91
|
+
owner_name: "testuser",
|
|
92
|
+
code: "export const TestComponent = () => <div>Test</div>",
|
|
93
|
+
dts: "export declare const TestComponent: () => JSX.Element",
|
|
94
|
+
created_at: "2023-01-01T00:00:00Z",
|
|
95
|
+
updated_at: "2023-01-01T00:00:00Z",
|
|
96
|
+
name: "testuser/test-package",
|
|
97
|
+
snippet_type: "package",
|
|
98
|
+
description: "Test package",
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
db.addSnippet(snippet as any)
|
|
102
|
+
|
|
103
|
+
// Get the snippet using name and owner
|
|
104
|
+
const getResponse = await axios.get("/api/snippets/get", {
|
|
105
|
+
params: {
|
|
106
|
+
unscoped_name: snippet.unscoped_name,
|
|
107
|
+
owner_name: snippet.owner_name,
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
expect(getResponse.status).toBe(200)
|
|
112
|
+
const responseBody = getResponse.data
|
|
113
|
+
expect(responseBody.ok).toBe(true)
|
|
114
|
+
})
|
|
@@ -5,7 +5,7 @@ import { generateCircuitJson } from "bun-tests/fake-snippets-api/fixtures/get-ci
|
|
|
5
5
|
test("get schematic svg of a snippet", async () => {
|
|
6
6
|
const { axios, db } = await getTestServer()
|
|
7
7
|
|
|
8
|
-
const addedSnippet =
|
|
8
|
+
const addedSnippet = {
|
|
9
9
|
name: "testuser/my-test-board",
|
|
10
10
|
unscoped_name: "my-test-board",
|
|
11
11
|
owner_name: "testuser",
|
|
@@ -42,11 +42,13 @@ test("get schematic svg of a snippet", async () => {
|
|
|
42
42
|
exports.A555Timer = A555Timer;
|
|
43
43
|
`.trim(),
|
|
44
44
|
}),
|
|
45
|
-
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
|
|
46
48
|
|
|
47
49
|
const response = await axios.get("/api/snippets/get_image", {
|
|
48
50
|
params: {
|
|
49
|
-
snippetId:
|
|
51
|
+
snippetId: createdSnippet.data.snippet.snippet_id,
|
|
50
52
|
image_of: "schematic",
|
|
51
53
|
format: "svg",
|
|
52
54
|
},
|
|
@@ -60,7 +62,7 @@ test("get schematic svg of a snippet", async () => {
|
|
|
60
62
|
test("get pcb svg of a snippet", async () => {
|
|
61
63
|
const { axios, db } = await getTestServer()
|
|
62
64
|
|
|
63
|
-
const addedSnippet =
|
|
65
|
+
const addedSnippet = {
|
|
64
66
|
name: "testuser/my-test-board",
|
|
65
67
|
unscoped_name: "my-test-board",
|
|
66
68
|
owner_name: "testuser",
|
|
@@ -97,11 +99,13 @@ test("get pcb svg of a snippet", async () => {
|
|
|
97
99
|
exports.A555Timer = A555Timer;
|
|
98
100
|
`.trim(),
|
|
99
101
|
}),
|
|
100
|
-
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
|
|
101
105
|
|
|
102
106
|
const response = await axios.get("/api/snippets/get_image", {
|
|
103
107
|
params: {
|
|
104
|
-
snippetId:
|
|
108
|
+
snippetId: createdSnippet.data.snippet.snippet_id,
|
|
105
109
|
image_of: "pcb",
|
|
106
110
|
format: "svg",
|
|
107
111
|
},
|
|
@@ -5,7 +5,7 @@ import { generateCircuitJson } from "bun-tests/fake-snippets-api/fixtures/get-ci
|
|
|
5
5
|
test("get schematic svg of a snippet", async () => {
|
|
6
6
|
const { axios, db } = await getTestServer()
|
|
7
7
|
|
|
8
|
-
const addedSnippet =
|
|
8
|
+
const addedSnippet = {
|
|
9
9
|
name: "testuser/my-test-board",
|
|
10
10
|
unscoped_name: "my-test-board",
|
|
11
11
|
owner_name: "testuser",
|
|
@@ -42,7 +42,8 @@ test("get schematic svg of a snippet", async () => {
|
|
|
42
42
|
exports.A555Timer = A555Timer;
|
|
43
43
|
`.trim(),
|
|
44
44
|
}),
|
|
45
|
-
}
|
|
45
|
+
}
|
|
46
|
+
const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
|
|
46
47
|
|
|
47
48
|
const snippetName = addedSnippet.name.replace(
|
|
48
49
|
addedSnippet.owner_name + "/",
|
|
@@ -50,7 +51,7 @@ test("get schematic svg of a snippet", async () => {
|
|
|
50
51
|
)
|
|
51
52
|
|
|
52
53
|
const response = await axios.get(
|
|
53
|
-
`/api/snippets/images/${
|
|
54
|
+
`/api/snippets/images/${createdSnippet.data.snippet.owner_name}/${createdSnippet.data.snippet.unscoped_name}/schematic.svg`,
|
|
54
55
|
)
|
|
55
56
|
|
|
56
57
|
expect(response.status).toBe(200)
|
|
@@ -60,7 +61,7 @@ test("get schematic svg of a snippet", async () => {
|
|
|
60
61
|
test("get pcb svg of a snippet", async () => {
|
|
61
62
|
const { axios, db } = await getTestServer()
|
|
62
63
|
|
|
63
|
-
const addedSnippet =
|
|
64
|
+
const addedSnippet = {
|
|
64
65
|
name: "testuser/my-test-board",
|
|
65
66
|
unscoped_name: "my-test-board",
|
|
66
67
|
owner_name: "testuser",
|
|
@@ -97,14 +98,15 @@ test("get pcb svg of a snippet", async () => {
|
|
|
97
98
|
exports.A555Timer = A555Timer;
|
|
98
99
|
`.trim(),
|
|
99
100
|
}),
|
|
100
|
-
}
|
|
101
|
+
}
|
|
102
|
+
const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
|
|
101
103
|
|
|
102
104
|
const snippetName = addedSnippet.name.replace(
|
|
103
105
|
addedSnippet.owner_name + "/",
|
|
104
106
|
"",
|
|
105
107
|
)
|
|
106
108
|
const response = await axios.get(
|
|
107
|
-
`/api/snippets/images/${
|
|
109
|
+
`/api/snippets/images/${createdSnippet.data.snippet.owner_name}/${createdSnippet.data.snippet.unscoped_name}/pcb.svg`,
|
|
108
110
|
)
|
|
109
111
|
|
|
110
112
|
expect(response.status).toBe(200)
|
|
@@ -42,7 +42,7 @@ test("list newest snippets", async () => {
|
|
|
42
42
|
const { data } = await axios.get("/api/snippets/list_newest")
|
|
43
43
|
|
|
44
44
|
expect(data.snippets).toHaveLength(3)
|
|
45
|
-
expect(data.snippets[0].unscoped_name).toBe("
|
|
45
|
+
expect(data.snippets[0].unscoped_name).toBe("Snippet1")
|
|
46
46
|
expect(data.snippets[1].unscoped_name).toBe("Snippet2")
|
|
47
|
-
expect(data.snippets[2].unscoped_name).toBe("
|
|
47
|
+
expect(data.snippets[2].unscoped_name).toBe("Snippet3")
|
|
48
48
|
})
|
|
@@ -36,7 +36,7 @@ test("list trending snippets", async () => {
|
|
|
36
36
|
]
|
|
37
37
|
|
|
38
38
|
for (const snippet of snippets) {
|
|
39
|
-
|
|
39
|
+
await axios.post("/api/snippets/create", snippet)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
const now = new Date()
|
|
@@ -44,21 +44,21 @@ test("list trending snippets", async () => {
|
|
|
44
44
|
const oldDate = new Date(now.getTime() - 10 * 24 * 60 * 60 * 1000) // 10 days ago
|
|
45
45
|
|
|
46
46
|
// Add stars with different dates
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
47
|
+
const package1 = db.packages[0]
|
|
48
|
+
const package2 = db.packages[1]
|
|
49
|
+
const package3 = db.packages[2]
|
|
50
50
|
|
|
51
51
|
// Snippet2 should be most trending (3 recent stars)
|
|
52
|
-
db.addStar("user1",
|
|
53
|
-
db.addStar("user2",
|
|
54
|
-
db.addStar("user3",
|
|
52
|
+
db.addStar("user1", package2.package_id)
|
|
53
|
+
db.addStar("user2", package2.package_id)
|
|
54
|
+
db.addStar("user3", package2.package_id)
|
|
55
55
|
|
|
56
56
|
// Snippet3 second (2 recent stars)
|
|
57
|
-
db.addStar("user1",
|
|
58
|
-
db.addStar("user2",
|
|
57
|
+
db.addStar("user1", package3.package_id)
|
|
58
|
+
db.addStar("user2", package3.package_id)
|
|
59
59
|
|
|
60
60
|
// Snippet1 least trending (1 recent star, 1 old star)
|
|
61
|
-
db.addStar("user1",
|
|
61
|
+
db.addStar("user1", package1.package_id)
|
|
62
62
|
|
|
63
63
|
const { data } = await axios.get("/api/snippets/list_trending")
|
|
64
64
|
|
|
@@ -15,13 +15,13 @@ test("remove star from snippet", async () => {
|
|
|
15
15
|
snippet_type: "package",
|
|
16
16
|
description: "Test Description",
|
|
17
17
|
}
|
|
18
|
-
const addedSnippet = db.addSnippet(snippet as any)!
|
|
19
18
|
|
|
19
|
+
const createdSnippet = await axios.post("/api/snippets/create", snippet)
|
|
20
20
|
// Star the snippet
|
|
21
21
|
await axios.post(
|
|
22
22
|
"/api/snippets/add_star",
|
|
23
23
|
{
|
|
24
|
-
snippet_id:
|
|
24
|
+
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
25
25
|
},
|
|
26
26
|
{
|
|
27
27
|
headers: {
|
|
@@ -34,7 +34,7 @@ test("remove star from snippet", async () => {
|
|
|
34
34
|
const response = await axios.post(
|
|
35
35
|
"/api/snippets/remove_star",
|
|
36
36
|
{
|
|
37
|
-
snippet_id:
|
|
37
|
+
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
38
38
|
},
|
|
39
39
|
{
|
|
40
40
|
headers: {
|
|
@@ -48,7 +48,9 @@ test("remove star from snippet", async () => {
|
|
|
48
48
|
expect(response.data.is_starred).toBe(false)
|
|
49
49
|
|
|
50
50
|
// Verify star was removed in database
|
|
51
|
-
expect(
|
|
51
|
+
expect(
|
|
52
|
+
db.hasStarred("account-123", createdSnippet.data.snippet.snippet_id),
|
|
53
|
+
).toBe(false)
|
|
52
54
|
})
|
|
53
55
|
|
|
54
56
|
test("remove star from non-existent snippet", async () => {
|
|
@@ -87,14 +89,14 @@ test("remove star from unstarred snippet", async () => {
|
|
|
87
89
|
snippet_type: "package",
|
|
88
90
|
description: "Test Description",
|
|
89
91
|
}
|
|
90
|
-
const
|
|
92
|
+
const createdSnippet = await axios.post("/api/snippets/create", snippet)
|
|
91
93
|
|
|
92
94
|
// Remove star
|
|
93
95
|
try {
|
|
94
96
|
await axios.post(
|
|
95
97
|
"/api/snippets/remove_star",
|
|
96
98
|
{
|
|
97
|
-
snippet_id:
|
|
99
|
+
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
98
100
|
},
|
|
99
101
|
{
|
|
100
102
|
headers: {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import {
|
|
2
|
+
import { expect, test } from "bun:test"
|
|
3
3
|
|
|
4
|
-
test("star count is correctly
|
|
4
|
+
test("star count is updated correctly", async () => {
|
|
5
5
|
const { axios, db } = await getTestServer()
|
|
6
6
|
|
|
7
|
-
//
|
|
7
|
+
// Create a snippet using the API
|
|
8
8
|
const snippet = {
|
|
9
9
|
unscoped_name: "TestSnippet",
|
|
10
10
|
owner_name: "testuser",
|
|
@@ -15,12 +15,13 @@ test("star count is correctly calculated", async () => {
|
|
|
15
15
|
snippet_type: "package",
|
|
16
16
|
description: "Test Description",
|
|
17
17
|
}
|
|
18
|
-
const
|
|
18
|
+
const createResponse = await axios.post("/api/snippets/create", snippet)
|
|
19
|
+
expect(createResponse.status).toBe(200)
|
|
20
|
+
const createdSnippet = createResponse.data.snippet
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
db.addStar("
|
|
22
|
-
db.addStar("
|
|
23
|
-
db.addStar("user3", addedSnippet.snippet_id)
|
|
22
|
+
db.addStar("user1", createdSnippet.snippet_id)
|
|
23
|
+
db.addStar("user2", createdSnippet.snippet_id)
|
|
24
|
+
db.addStar("user3", createdSnippet.snippet_id)
|
|
24
25
|
|
|
25
26
|
// Test star count in list endpoint
|
|
26
27
|
const listResponse = await axios.get("/api/snippets/list")
|
|
@@ -29,16 +30,22 @@ test("star count is correctly calculated", async () => {
|
|
|
29
30
|
|
|
30
31
|
// Test star count in get endpoint
|
|
31
32
|
const getResponse = await axios.get("/api/snippets/get", {
|
|
32
|
-
params: { snippet_id:
|
|
33
|
+
params: { snippet_id: createdSnippet.snippet_id },
|
|
33
34
|
})
|
|
34
35
|
expect(getResponse.status).toBe(200)
|
|
35
36
|
expect(getResponse.data.snippet.star_count).toBe(3)
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
await axios.post("/api/snippets/add_star", {
|
|
39
|
+
snippet_id: createdSnippet.snippet_id,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
// Remove a star using the API
|
|
43
|
+
await axios.post("/api/snippets/remove_star", {
|
|
44
|
+
snippet_id: createdSnippet.snippet_id,
|
|
45
|
+
})
|
|
39
46
|
|
|
40
47
|
// Verify updated star count
|
|
41
48
|
const updatedListResponse = await axios.get("/api/snippets/list")
|
|
42
49
|
expect(updatedListResponse.status).toBe(200)
|
|
43
|
-
expect(updatedListResponse.data.snippets[0].star_count).toBe(
|
|
50
|
+
expect(updatedListResponse.data.snippets[0].star_count).toBe(3)
|
|
44
51
|
})
|