@tscircuit/fake-snippets 0.0.66 → 0.0.67
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/fixtures/get-circuit-json.ts +5 -143
- package/bun-tests/fake-snippets-api/fixtures/get-test-server.ts +1 -4
- package/bun-tests/fake-snippets-api/fixtures/start-server.ts +7 -3
- package/bun-tests/fake-snippets-api/routes/order_quotes/create.test.ts +20 -56
- package/bun-tests/fake-snippets-api/routes/package_files/create_or_update.test.ts +2 -2
- package/bun-tests/fake-snippets-api/routes/package_releases/update.test.ts +1 -1
- package/bun-tests/fake-snippets-api/routes/packages/images.test.ts +0 -11
- package/bun.lock +15 -17
- package/dist/bundle.js +32 -39
- package/fake-snippets-api/routes/api/order_quotes/create.ts +30 -37
- package/fake-snippets-api/routes/api/order_quotes/get.ts +5 -8
- package/package.json +4 -3
- package/src/App.tsx +0 -7
- package/src/ContextProviders.tsx +2 -0
- package/src/components/DownloadButtonAndMenu.tsx +1 -4
- package/src/components/Footer.tsx +5 -2
- package/src/components/HeaderLogin.tsx +37 -54
- package/src/components/ImageWithFallback.tsx +37 -0
- package/src/components/JLCPCBImportDialog.tsx +43 -24
- package/src/components/PackageCard.tsx +2 -2
- package/src/components/{SnippetLink.tsx → PackageLink.tsx} +8 -16
- package/src/components/PackageSearchResults.tsx +87 -0
- package/src/components/PackagesList.tsx +3 -3
- package/src/components/PageSearchComponent.tsx +9 -9
- package/src/components/ViewPackagePage/components/ShikiCodeViewer.tsx +5 -28
- package/src/components/ViewPackagePage/components/main-content-header.tsx +8 -8
- package/src/components/ViewPackagePage/components/mobile-sidebar.tsx +24 -14
- package/src/components/ViewPackagePage/components/package-header.tsx +6 -1
- package/src/components/package-port/CodeEditor.tsx +13 -10
- package/src/components/package-port/CodeEditorHeader.tsx +1 -1
- package/src/components/package-port/EditorNav.tsx +2 -2
- package/src/hooks/use-global-store.ts +1 -0
- package/src/hooks/use-shiki-highlighter.ts +13 -6
- package/src/lib/download-fns/download-gltf.ts +3 -10
- package/src/lib/handleManualEditsImport.tsx +1 -1
- package/src/lib/types.ts +4 -2
- package/src/pages/dashboard.tsx +3 -4
- package/src/pages/editor.tsx +20 -14
- package/src/pages/latest.tsx +25 -26
- package/src/pages/search.tsx +120 -19
- package/src/pages/trending.tsx +14 -58
- package/src/pages/user-profile.tsx +13 -8
- package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +0 -84
- package/bun-tests/fake-snippets-api/routes/snippets/create.test.ts +0 -53
- package/bun-tests/fake-snippets-api/routes/snippets/delete.test.ts +0 -82
- package/bun-tests/fake-snippets-api/routes/snippets/download.test.ts +0 -90
- package/bun-tests/fake-snippets-api/routes/snippets/generate_from_jlcpcb.test.ts +0 -16
- package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +0 -163
- package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +0 -117
- package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +0 -114
- package/bun-tests/fake-snippets-api/routes/snippets/list.test.ts +0 -169
- package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +0 -50
- package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +0 -72
- package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +0 -80
- package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +0 -75
- package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +0 -51
- package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +0 -175
- package/src/components/AiChatInterface.tsx +0 -229
- package/src/components/CodeAndPreview.tsx +0 -289
- package/src/components/CodeEditor.tsx +0 -539
- package/src/components/CodeEditorHeader.tsx +0 -135
- package/src/components/EditorNav.tsx +0 -502
- package/src/components/PreviewContent.tsx +0 -372
- package/src/components/SnippetCard.tsx +0 -159
- package/src/components/SnippetList.tsx +0 -71
- package/src/hooks/use-compiled-tsx.ts +0 -37
- package/src/hooks/use-run-tsx/construct-circuit.tsx +0 -62
- package/src/hooks/use-run-tsx/index.tsx +0 -256
- package/src/hooks/use-save-snippet.ts +0 -66
- package/src/hooks/use-typecheck.ts +0 -54
- package/src/lib/utils/getSyntaxError.ts +0 -13
- package/src/pages/ai.tsx +0 -92
- package/src/pages/view-snippet.tsx +0 -166
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { test, expect } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("list latest snippets", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Add some test snippets
|
|
8
|
-
const snippets = [
|
|
9
|
-
{
|
|
10
|
-
unscoped_name: "Snippet1",
|
|
11
|
-
owner_name: "User1",
|
|
12
|
-
code: "Content1",
|
|
13
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
15
|
-
name: "User1/Snippet1",
|
|
16
|
-
snippet_type: "board",
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
unscoped_name: "Snippet2",
|
|
20
|
-
owner_name: "User2",
|
|
21
|
-
code: "Content2",
|
|
22
|
-
created_at: "2023-01-02T00:00:00Z",
|
|
23
|
-
updated_at: "2023-01-02T00:00:00Z",
|
|
24
|
-
name: "User2/Snippet2",
|
|
25
|
-
snippet_type: "package",
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
unscoped_name: "Snippet3",
|
|
29
|
-
owner_name: "User3",
|
|
30
|
-
code: "Content3",
|
|
31
|
-
created_at: "2023-01-03T00:00:00Z",
|
|
32
|
-
updated_at: "2023-01-03T00:00:00Z",
|
|
33
|
-
name: "User3/Snippet3",
|
|
34
|
-
snippet_type: "model",
|
|
35
|
-
},
|
|
36
|
-
]
|
|
37
|
-
|
|
38
|
-
for (const snippet of snippets) {
|
|
39
|
-
db.addSnippet(snippet as any)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const { data } = await axios.get("/api/snippets/list_latest")
|
|
43
|
-
|
|
44
|
-
expect(data.snippets).toHaveLength(3)
|
|
45
|
-
// Order might vary in test runs, just check all snippets are there
|
|
46
|
-
const names = data.snippets.map((s: any) => s.unscoped_name)
|
|
47
|
-
expect(names).toContain("Snippet1")
|
|
48
|
-
expect(names).toContain("Snippet2")
|
|
49
|
-
expect(names).toContain("Snippet3")
|
|
50
|
-
})
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { test, expect } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("list trending snippets", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Add some test snippets
|
|
8
|
-
const snippets = [
|
|
9
|
-
{
|
|
10
|
-
unscoped_name: "Snippet1",
|
|
11
|
-
owner_name: "User1",
|
|
12
|
-
code: "Content1",
|
|
13
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
15
|
-
name: "User1/Snippet1",
|
|
16
|
-
snippet_type: "board",
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
unscoped_name: "Snippet2",
|
|
20
|
-
owner_name: "User2",
|
|
21
|
-
code: "Content2",
|
|
22
|
-
created_at: "2023-01-02T00:00:00Z",
|
|
23
|
-
updated_at: "2023-01-02T00:00:00Z",
|
|
24
|
-
name: "User2/Snippet2",
|
|
25
|
-
snippet_type: "package",
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
unscoped_name: "Snippet3",
|
|
29
|
-
owner_name: "User3",
|
|
30
|
-
code: "Content3",
|
|
31
|
-
created_at: "2023-01-03T00:00:00Z",
|
|
32
|
-
updated_at: "2023-01-03T00:00:00Z",
|
|
33
|
-
name: "User3/Snippet3",
|
|
34
|
-
snippet_type: "model",
|
|
35
|
-
},
|
|
36
|
-
]
|
|
37
|
-
|
|
38
|
-
for (const snippet of snippets) {
|
|
39
|
-
await axios.post("/api/snippets/create", snippet)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const now = new Date()
|
|
43
|
-
const recentDate = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000) // 3 days ago
|
|
44
|
-
const oldDate = new Date(now.getTime() - 10 * 24 * 60 * 60 * 1000) // 10 days ago
|
|
45
|
-
|
|
46
|
-
// Add stars with different dates
|
|
47
|
-
const package1 = db.packages[0]
|
|
48
|
-
const package2 = db.packages[1]
|
|
49
|
-
const package3 = db.packages[2]
|
|
50
|
-
|
|
51
|
-
// Set star counts directly on package objects
|
|
52
|
-
package1.star_count = 1
|
|
53
|
-
package2.star_count = 3
|
|
54
|
-
package3.star_count = 2
|
|
55
|
-
|
|
56
|
-
// Add stars to match
|
|
57
|
-
db.addStar("user1", package1.package_id)
|
|
58
|
-
|
|
59
|
-
db.addStar("user1", package2.package_id)
|
|
60
|
-
db.addStar("user2", package2.package_id)
|
|
61
|
-
db.addStar("user3", package2.package_id)
|
|
62
|
-
|
|
63
|
-
db.addStar("user1", package3.package_id)
|
|
64
|
-
db.addStar("user2", package3.package_id)
|
|
65
|
-
|
|
66
|
-
const { data } = await axios.get("/api/snippets/list_trending")
|
|
67
|
-
|
|
68
|
-
expect(data.snippets).toHaveLength(3)
|
|
69
|
-
expect(data.snippets[0].unscoped_name).toBe("Snippet2") // Most stars
|
|
70
|
-
expect(data.snippets[1].unscoped_name).toBe("Snippet3") // Second most
|
|
71
|
-
expect(data.snippets[2].unscoped_name).toBe("Snippet1") // Least stars
|
|
72
|
-
})
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { test, expect } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("remove star from snippet", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Add a test snippet
|
|
8
|
-
const snippet = {
|
|
9
|
-
unscoped_name: "TestSnippet",
|
|
10
|
-
owner_name: "otheruser",
|
|
11
|
-
code: "Test Content",
|
|
12
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
13
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
name: "otheruser/TestSnippet",
|
|
15
|
-
snippet_type: "package",
|
|
16
|
-
description: "Test Description",
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const createdSnippet = await axios.post("/api/snippets/create", snippet)
|
|
20
|
-
// Star the snippet
|
|
21
|
-
await axios.post("/api/snippets/add_star", {
|
|
22
|
-
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
// remove star from snippet
|
|
26
|
-
const response = await axios.post("/api/snippets/remove_star", {
|
|
27
|
-
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
expect(response.status).toBe(200)
|
|
31
|
-
expect(response.data.ok).toBe(true)
|
|
32
|
-
expect(response.data.is_starred).toBe(false)
|
|
33
|
-
|
|
34
|
-
// Verify star was removed in database
|
|
35
|
-
expect(
|
|
36
|
-
db.hasStarred("account-123", createdSnippet.data.snippet.snippet_id),
|
|
37
|
-
).toBe(false)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
test("remove star from non-existent snippet", async () => {
|
|
41
|
-
const { axios } = await getTestServer()
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
await axios.post("/api/snippets/remove_star", {
|
|
45
|
-
snippet_id: "non-existent-id",
|
|
46
|
-
})
|
|
47
|
-
expect(true).toBe(false) // Should not reach here
|
|
48
|
-
} catch (error: any) {
|
|
49
|
-
expect(error.status).toBe(404)
|
|
50
|
-
expect(error.data.error.message).toBe("Snippet not found")
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test("remove star from unstarred snippet", async () => {
|
|
55
|
-
const { axios, db } = await getTestServer()
|
|
56
|
-
|
|
57
|
-
// Add a test snippet
|
|
58
|
-
const snippet = {
|
|
59
|
-
unscoped_name: "TestSnippet",
|
|
60
|
-
owner_name: "otheruser",
|
|
61
|
-
code: "Test Content",
|
|
62
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
63
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
64
|
-
name: "otheruser/TestSnippet",
|
|
65
|
-
snippet_type: "package",
|
|
66
|
-
description: "Test Description",
|
|
67
|
-
}
|
|
68
|
-
const createdSnippet = await axios.post("/api/snippets/create", snippet)
|
|
69
|
-
|
|
70
|
-
// Remove star
|
|
71
|
-
try {
|
|
72
|
-
await axios.post("/api/snippets/remove_star", {
|
|
73
|
-
snippet_id: createdSnippet.data.snippet.snippet_id,
|
|
74
|
-
})
|
|
75
|
-
expect(true).toBe(false) // Should not reach here
|
|
76
|
-
} catch (error: any) {
|
|
77
|
-
expect(error.status).toBe(400)
|
|
78
|
-
expect(error.data.error.message).toBe("You have not starred this snippet")
|
|
79
|
-
}
|
|
80
|
-
})
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { expect, test } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("search snippets", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Add some test snippets
|
|
8
|
-
const snippets = [
|
|
9
|
-
{
|
|
10
|
-
unscoped_name: "Snippet1",
|
|
11
|
-
owner_name: "User1",
|
|
12
|
-
code: "Content1",
|
|
13
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
15
|
-
name: "User1/Snippet1",
|
|
16
|
-
snippet_type: "board",
|
|
17
|
-
description: "First test snippet",
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
unscoped_name: "Snippet2",
|
|
21
|
-
owner_name: "User2",
|
|
22
|
-
code: "Content2 with searchable text",
|
|
23
|
-
created_at: "2023-01-02T00:00:00Z",
|
|
24
|
-
updated_at: "2023-01-02T00:00:00Z",
|
|
25
|
-
name: "User2/Snippet2",
|
|
26
|
-
snippet_type: "package",
|
|
27
|
-
description: "Second test snippet",
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
unscoped_name: "Snippet3",
|
|
31
|
-
owner_name: "User1",
|
|
32
|
-
code: "Content3",
|
|
33
|
-
created_at: "2023-01-03T00:00:00Z",
|
|
34
|
-
updated_at: "2023-01-03T00:00:00Z",
|
|
35
|
-
name: "User1/Snippet3",
|
|
36
|
-
snippet_type: "model",
|
|
37
|
-
description: "Third test snippet with searchable description",
|
|
38
|
-
},
|
|
39
|
-
]
|
|
40
|
-
|
|
41
|
-
for (const snippet of snippets) {
|
|
42
|
-
db.addSnippet(snippet as any)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Test search by name
|
|
46
|
-
const nameSearchResponse = await axios.get("/api/snippets/search", {
|
|
47
|
-
params: { q: "Snippet1" },
|
|
48
|
-
})
|
|
49
|
-
expect(nameSearchResponse.status).toBe(200)
|
|
50
|
-
expect(nameSearchResponse.data.snippets).toHaveLength(1)
|
|
51
|
-
expect(nameSearchResponse.data.snippets[0].name).toBe("User1/Snippet1")
|
|
52
|
-
|
|
53
|
-
// Test search by description
|
|
54
|
-
const descriptionSearchResponse = await axios.get("/api/snippets/search", {
|
|
55
|
-
params: { q: "searchable description" },
|
|
56
|
-
})
|
|
57
|
-
expect(descriptionSearchResponse.status).toBe(200)
|
|
58
|
-
expect(descriptionSearchResponse.data.snippets).toHaveLength(1)
|
|
59
|
-
expect(descriptionSearchResponse.data.snippets[0].name).toBe("User1/Snippet3")
|
|
60
|
-
|
|
61
|
-
// Test search by code content
|
|
62
|
-
const codeSearchResponse = await axios.get("/api/snippets/search", {
|
|
63
|
-
params: { q: "searchable text" },
|
|
64
|
-
})
|
|
65
|
-
expect(codeSearchResponse.status).toBe(200)
|
|
66
|
-
expect(codeSearchResponse.data.snippets).toHaveLength(1)
|
|
67
|
-
expect(codeSearchResponse.data.snippets[0].name).toBe("User2/Snippet2")
|
|
68
|
-
|
|
69
|
-
// Test search with no results
|
|
70
|
-
const noResultsResponse = await axios.get("/api/snippets/search", {
|
|
71
|
-
params: { q: "nonexistent" },
|
|
72
|
-
})
|
|
73
|
-
expect(noResultsResponse.status).toBe(200)
|
|
74
|
-
expect(noResultsResponse.data.snippets).toHaveLength(0)
|
|
75
|
-
})
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { expect, test } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("star count is updated correctly", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Create a snippet using the API
|
|
8
|
-
const snippet = {
|
|
9
|
-
unscoped_name: "TestSnippet",
|
|
10
|
-
owner_name: "testuser",
|
|
11
|
-
code: "Test Content",
|
|
12
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
13
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
name: "testuser/TestSnippet",
|
|
15
|
-
snippet_type: "package",
|
|
16
|
-
description: "Test Description",
|
|
17
|
-
}
|
|
18
|
-
const createResponse = await axios.post("/api/snippets/create", snippet)
|
|
19
|
-
expect(createResponse.status).toBe(200)
|
|
20
|
-
const createdSnippet = createResponse.data.snippet
|
|
21
|
-
|
|
22
|
-
db.addStar("user1", createdSnippet.snippet_id)
|
|
23
|
-
db.addStar("user2", createdSnippet.snippet_id)
|
|
24
|
-
db.addStar("user3", createdSnippet.snippet_id)
|
|
25
|
-
|
|
26
|
-
// Test star count in list endpoint
|
|
27
|
-
const listResponse = await axios.get("/api/snippets/list")
|
|
28
|
-
expect(listResponse.status).toBe(200)
|
|
29
|
-
expect(listResponse.data.snippets[0].star_count).toBe(3)
|
|
30
|
-
|
|
31
|
-
// Test star count in get endpoint
|
|
32
|
-
const getResponse = await axios.get("/api/snippets/get", {
|
|
33
|
-
params: { snippet_id: createdSnippet.snippet_id },
|
|
34
|
-
})
|
|
35
|
-
expect(getResponse.status).toBe(200)
|
|
36
|
-
expect(getResponse.data.snippet.star_count).toBe(3)
|
|
37
|
-
|
|
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
|
-
})
|
|
46
|
-
|
|
47
|
-
// Verify updated star count
|
|
48
|
-
const updatedListResponse = await axios.get("/api/snippets/list")
|
|
49
|
-
expect(updatedListResponse.status).toBe(200)
|
|
50
|
-
expect(updatedListResponse.data.snippets[0].star_count).toBe(3)
|
|
51
|
-
})
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
|
|
2
|
-
import { test, expect } from "bun:test"
|
|
3
|
-
|
|
4
|
-
test("update snippet", async () => {
|
|
5
|
-
const { axios, db } = await getTestServer()
|
|
6
|
-
|
|
7
|
-
// Add a test snippet
|
|
8
|
-
const snippet = {
|
|
9
|
-
unscoped_name: "TestSnippet",
|
|
10
|
-
owner_name: "testuser",
|
|
11
|
-
code: "Original Content",
|
|
12
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
13
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
14
|
-
name: "testuser/TestSnippet",
|
|
15
|
-
snippet_type: "package",
|
|
16
|
-
description: "Original Description",
|
|
17
|
-
compiled_js: null,
|
|
18
|
-
}
|
|
19
|
-
db.addSnippet(snippet as any)
|
|
20
|
-
|
|
21
|
-
const addedPackage = db.packages[0]
|
|
22
|
-
|
|
23
|
-
// Update the snippet
|
|
24
|
-
const updatedCode = "Updated Content"
|
|
25
|
-
const updatedCompiledJs = "console.log('Updated Content')"
|
|
26
|
-
const response = await axios.post("/api/snippets/update", {
|
|
27
|
-
snippet_id: addedPackage.package_id,
|
|
28
|
-
code: updatedCode,
|
|
29
|
-
compiled_js: updatedCompiledJs,
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
expect(response.status).toBe(200)
|
|
33
|
-
expect(response.data.snippet.code).toBe(updatedCode)
|
|
34
|
-
expect(response.data.snippet.compiled_js).toBe(updatedCompiledJs)
|
|
35
|
-
expect(response.data.snippet.updated_at).not.toBe(addedPackage.created_at)
|
|
36
|
-
|
|
37
|
-
// Verify the snippet was updated in the database
|
|
38
|
-
const updatedPackageFiles = db.packageFiles.filter(
|
|
39
|
-
(p) => p.package_release_id === addedPackage.latest_package_release_id,
|
|
40
|
-
)
|
|
41
|
-
expect(updatedPackageFiles.length).toBe(3)
|
|
42
|
-
expect(updatedPackageFiles[0].content_text).toBe(updatedCode)
|
|
43
|
-
expect(updatedPackageFiles[1].content_text).toBe("") // dts
|
|
44
|
-
expect(updatedPackageFiles[2].content_text).toBe(updatedCompiledJs)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test("update non-existent snippet", async () => {
|
|
48
|
-
const { axios } = await getTestServer()
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
await axios.post("/api/snippets/update", {
|
|
52
|
-
snippet_id: "non-existent-id",
|
|
53
|
-
code: "Updated Content",
|
|
54
|
-
compiled_js: "console.log('Updated Content')",
|
|
55
|
-
})
|
|
56
|
-
// If the request doesn't throw an error, fail the test
|
|
57
|
-
expect(true).toBe(false)
|
|
58
|
-
} catch (error: any) {
|
|
59
|
-
expect(error.status).toBe(404)
|
|
60
|
-
expect(error.data.error.message).toBe("Snippet not found")
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test("update snippet with null compiled_js", async () => {
|
|
65
|
-
const { axios, db } = await getTestServer()
|
|
66
|
-
|
|
67
|
-
// Add a test snippet with compiled_js
|
|
68
|
-
const snippet = {
|
|
69
|
-
unscoped_name: "TestSnippet",
|
|
70
|
-
owner_name: "testuser",
|
|
71
|
-
code: "Original Content",
|
|
72
|
-
created_at: "2023-01-01T00:00:00Z",
|
|
73
|
-
updated_at: "2023-01-01T00:00:00Z",
|
|
74
|
-
name: "testuser/TestSnippet",
|
|
75
|
-
snippet_type: "package",
|
|
76
|
-
description: "Original Description",
|
|
77
|
-
compiled_js: "console.log('Original Content')",
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
db.addSnippet(snippet as any)
|
|
81
|
-
|
|
82
|
-
const addedPackage = db.packages[0]
|
|
83
|
-
|
|
84
|
-
// Update the snippet with null compiled_js
|
|
85
|
-
const response = await axios.post("/api/snippets/update", {
|
|
86
|
-
snippet_id: addedPackage.package_id,
|
|
87
|
-
compiled_js: "",
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
expect(response.status).toBe(200)
|
|
91
|
-
expect(response.data.snippet.compiled_js).toBeEmpty()
|
|
92
|
-
|
|
93
|
-
// Verify the snippet was updated in the database
|
|
94
|
-
const updatedPackageFiles = db.packageFiles.filter(
|
|
95
|
-
(p) => p.package_release_id === addedPackage.latest_package_release_id,
|
|
96
|
-
)
|
|
97
|
-
expect(updatedPackageFiles.length).toBe(3)
|
|
98
|
-
expect(updatedPackageFiles[0].content_text).toBe(snippet.code)
|
|
99
|
-
expect(updatedPackageFiles[1].content_text).toBe("")
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
test("update snippet after create snippet", async () => {
|
|
103
|
-
const { axios, db } = await getTestServer()
|
|
104
|
-
|
|
105
|
-
const snippet = {
|
|
106
|
-
unscoped_name: "TestSnippet",
|
|
107
|
-
code: "Test Content",
|
|
108
|
-
snippet_type: "package",
|
|
109
|
-
description: "Test Description",
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await axios.post("/api/snippets/create", snippet)
|
|
113
|
-
|
|
114
|
-
const createdSnippet = db.packages[0]
|
|
115
|
-
|
|
116
|
-
const updatedCode = "Updated Content"
|
|
117
|
-
const response = await axios.post("/api/snippets/update", {
|
|
118
|
-
snippet_id: createdSnippet.package_id,
|
|
119
|
-
code: updatedCode,
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
expect(response.status).toBe(200)
|
|
123
|
-
expect(response.data.snippet.code).toBe(updatedCode)
|
|
124
|
-
// Don't check updated_at timestamp as it might not change in tests
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
test("update snippet visibility", async () => {
|
|
128
|
-
const { axios, db } = await getTestServer()
|
|
129
|
-
|
|
130
|
-
const snippet = {
|
|
131
|
-
unscoped_name: "TestSnippet",
|
|
132
|
-
code: "Test Content",
|
|
133
|
-
snippet_type: "package",
|
|
134
|
-
description: "Test Description",
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
await axios.post("/api/snippets/create", snippet)
|
|
138
|
-
|
|
139
|
-
const createdSnippet = db.packages[0]
|
|
140
|
-
|
|
141
|
-
const response = await axios.post("/api/snippets/update", {
|
|
142
|
-
snippet_id: createdSnippet.package_id,
|
|
143
|
-
is_private: true,
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
expect(response.status).toBe(200)
|
|
147
|
-
expect(response.data.snippet.is_private).toBe(true)
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
test("update snippet visibility with unauthenticated user", async () => {
|
|
151
|
-
const { axios, db } = await getTestServer()
|
|
152
|
-
|
|
153
|
-
const snippet = {
|
|
154
|
-
unscoped_name: "TestSnippet",
|
|
155
|
-
code: "Test Content",
|
|
156
|
-
snippet_type: "package",
|
|
157
|
-
description: "Test Description",
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
db.addSnippet(snippet as any)
|
|
161
|
-
|
|
162
|
-
const createdSnippet = db.packages[0]
|
|
163
|
-
|
|
164
|
-
try {
|
|
165
|
-
await axios.post("/api/snippets/update", {
|
|
166
|
-
snippet_id: createdSnippet.package_id,
|
|
167
|
-
is_private: true,
|
|
168
|
-
})
|
|
169
|
-
} catch (error: any) {
|
|
170
|
-
expect(error.status).toBe(403)
|
|
171
|
-
expect(error.data.error.message).toBe(
|
|
172
|
-
"You don't have permission to update this snippet",
|
|
173
|
-
)
|
|
174
|
-
}
|
|
175
|
-
})
|