@tscircuit/fake-snippets 0.0.65 → 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.
Files changed (87) hide show
  1. package/CONTRIBUTING.md +2 -2
  2. package/README.md +2 -2
  3. package/bun-tests/fake-snippets-api/fixtures/get-circuit-json.ts +5 -143
  4. package/bun-tests/fake-snippets-api/fixtures/get-test-server.ts +1 -4
  5. package/bun-tests/fake-snippets-api/fixtures/start-server.ts +7 -3
  6. package/bun-tests/fake-snippets-api/routes/order_quotes/create.test.ts +20 -56
  7. package/bun-tests/fake-snippets-api/routes/package_files/create_or_update.test.ts +2 -2
  8. package/bun-tests/fake-snippets-api/routes/package_releases/update.test.ts +1 -1
  9. package/bun-tests/fake-snippets-api/routes/packages/images.test.ts +1 -16
  10. package/bun.lock +42 -31
  11. package/dist/bundle.js +34 -41
  12. package/fake-snippets-api/routes/api/order_quotes/create.ts +30 -37
  13. package/fake-snippets-api/routes/api/order_quotes/get.ts +5 -8
  14. package/fake-snippets-api/routes/api/packages/images/[owner_github_username]/[unscoped_name]/[view_format].ts +3 -3
  15. package/package.json +7 -5
  16. package/src/App.tsx +0 -7
  17. package/src/ContextProviders.tsx +2 -0
  18. package/src/components/DownloadButtonAndMenu.tsx +1 -4
  19. package/src/components/ErrorTabContent.tsx +1 -1
  20. package/src/components/Footer.tsx +5 -2
  21. package/src/components/HeaderLogin.tsx +37 -54
  22. package/src/components/ImageWithFallback.tsx +37 -0
  23. package/src/components/JLCPCBImportDialog.tsx +43 -24
  24. package/src/components/PackageCard.tsx +12 -3
  25. package/src/components/{SnippetLink.tsx → PackageLink.tsx} +8 -16
  26. package/src/components/PackageSearchResults.tsx +87 -0
  27. package/src/components/PackagesList.tsx +3 -3
  28. package/src/components/PageSearchComponent.tsx +9 -9
  29. package/src/components/ShippingInformationForm.tsx +1 -1
  30. package/src/components/TrendingPackagesCarousel.tsx +43 -23
  31. package/src/components/ViewPackagePage/components/ShikiCodeViewer.tsx +5 -28
  32. package/src/components/ViewPackagePage/components/main-content-header.tsx +10 -22
  33. package/src/components/ViewPackagePage/components/mobile-sidebar.tsx +25 -14
  34. package/src/components/ViewPackagePage/components/package-header.tsx +9 -4
  35. package/src/components/ViewPackagePage/components/preview-image-squares.tsx +6 -1
  36. package/src/components/ViewPackagePage/components/repo-page-content.tsx +4 -4
  37. package/src/components/ViewPackagePage/components/sidebar.tsx +2 -14
  38. package/src/components/ViewSnippetSidebar.tsx +1 -1
  39. package/src/components/package-port/CodeEditor.tsx +13 -10
  40. package/src/components/package-port/CodeEditorHeader.tsx +1 -1
  41. package/src/components/package-port/EditorNav.tsx +2 -2
  42. package/src/hooks/use-get-fsmap-hash-for-package.ts +19 -0
  43. package/src/hooks/use-global-store.ts +1 -0
  44. package/src/hooks/use-preview-images.ts +20 -4
  45. package/src/hooks/use-shiki-highlighter.ts +13 -6
  46. package/src/hooks/use-toast.tsx +1 -1
  47. package/src/lib/download-fns/download-gltf.ts +3 -10
  48. package/src/lib/handleManualEditsImport.tsx +1 -1
  49. package/src/lib/types.ts +4 -2
  50. package/src/pages/dashboard.tsx +3 -4
  51. package/src/pages/editor.tsx +20 -14
  52. package/src/pages/latest.tsx +25 -26
  53. package/src/pages/package-editor.tsx +14 -2
  54. package/src/pages/search.tsx +120 -19
  55. package/src/pages/trending.tsx +14 -59
  56. package/src/pages/user-profile.tsx +13 -8
  57. package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +0 -84
  58. package/bun-tests/fake-snippets-api/routes/snippets/create.test.ts +0 -53
  59. package/bun-tests/fake-snippets-api/routes/snippets/delete.test.ts +0 -82
  60. package/bun-tests/fake-snippets-api/routes/snippets/download.test.ts +0 -90
  61. package/bun-tests/fake-snippets-api/routes/snippets/generate_from_jlcpcb.test.ts +0 -16
  62. package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +0 -163
  63. package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +0 -117
  64. package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +0 -114
  65. package/bun-tests/fake-snippets-api/routes/snippets/list.test.ts +0 -169
  66. package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +0 -50
  67. package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +0 -72
  68. package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +0 -80
  69. package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +0 -75
  70. package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +0 -51
  71. package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +0 -175
  72. package/src/components/AiChatInterface.tsx +0 -229
  73. package/src/components/CodeAndPreview.tsx +0 -289
  74. package/src/components/CodeEditor.tsx +0 -539
  75. package/src/components/CodeEditorHeader.tsx +0 -135
  76. package/src/components/EditorNav.tsx +0 -502
  77. package/src/components/PreviewContent.tsx +0 -372
  78. package/src/components/SnippetCard.tsx +0 -159
  79. package/src/components/SnippetList.tsx +0 -71
  80. package/src/hooks/use-compiled-tsx.ts +0 -37
  81. package/src/hooks/use-run-tsx/construct-circuit.tsx +0 -62
  82. package/src/hooks/use-run-tsx/index.tsx +0 -256
  83. package/src/hooks/use-save-snippet.ts +0 -66
  84. package/src/hooks/use-typecheck.ts +0 -54
  85. package/src/lib/utils/getSyntaxError.ts +0 -13
  86. package/src/pages/ai.tsx +0 -92
  87. package/src/pages/view-snippet.tsx +0 -166
@@ -1,117 +0,0 @@
1
- import { expect, test } from "bun:test"
2
- import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
3
- import { generateCircuitJson } from "bun-tests/fake-snippets-api/fixtures/get-circuit-json"
4
-
5
- test("get schematic svg of a snippet", async () => {
6
- const { axios, db } = await getTestServer()
7
-
8
- const addedSnippet = {
9
- name: "testuser/my-test-board",
10
- unscoped_name: "my-test-board",
11
- owner_name: "testuser",
12
- code: `
13
- import { A555Timer } from "@tsci/seveibar.a555timer"
14
-
15
- export default () => (
16
- <board width="10mm" height="10mm">
17
- <A555Timer name="U1" />
18
- </board>
19
- )`.trim(),
20
- created_at: new Date().toISOString(),
21
- updated_at: new Date().toISOString(),
22
- snippet_type: "board",
23
- description: "A simple board with an A555 Timer component",
24
- circuit_json: await generateCircuitJson({
25
- code: `
26
- import { A555Timer } from "@tsci/seveibar.a555timer"
27
-
28
- export default () => (
29
- <board width="10mm" height="10mm">
30
- <A555Timer name="U1" />
31
- </board>
32
- )`.trim(),
33
- type: "board",
34
- compiled_js: `
35
- "use strict";
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.A555Timer = void 0;
38
- const A555Timer = ({ name }) => /*#__PURE__*/React.createElement("chip", {
39
- name: name,
40
- footprint: "dip8"
41
- });
42
- exports.A555Timer = A555Timer;
43
- `.trim(),
44
- }),
45
- }
46
-
47
- const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
48
-
49
- const response = await axios.get("/api/snippets/get_image", {
50
- params: {
51
- snippetId: createdSnippet.data.snippet.snippet_id,
52
- image_of: "schematic",
53
- format: "svg",
54
- },
55
- })
56
-
57
- expect(response.status).toBe(200)
58
- expect(response.data.ok).toBe(true)
59
- expect(response.data.svg).toContain("<svg")
60
- })
61
-
62
- test("get pcb svg of a snippet", async () => {
63
- const { axios, db } = await getTestServer()
64
-
65
- const addedSnippet = {
66
- name: "testuser/my-test-board",
67
- unscoped_name: "my-test-board",
68
- owner_name: "testuser",
69
- code: `
70
- import { A555Timer } from "@tsci/seveibar.a555timer"
71
-
72
- export default () => (
73
- <board width="10mm" height="10mm">
74
- <A555Timer name="U1" />
75
- </board>
76
- )`.trim(),
77
- created_at: new Date().toISOString(),
78
- updated_at: new Date().toISOString(),
79
- snippet_type: "board",
80
- description: "A simple board with an A555 Timer component",
81
- circuit_json: await generateCircuitJson({
82
- code: `
83
- import { A555Timer } from "@tsci/seveibar.a555timer"
84
-
85
- export default () => (
86
- <board width="10mm" height="10mm">
87
- <A555Timer name="U1" />
88
- </board>
89
- )`.trim(),
90
- type: "board",
91
- compiled_js: `
92
- "use strict";
93
- Object.defineProperty(exports, "__esModule", { value: true });
94
- exports.A555Timer = void 0;
95
- const A555Timer = ({ name }) => /*#__PURE__*/React.createElement("chip", {
96
- name: name,
97
- footprint: "dip8"
98
- });
99
- exports.A555Timer = A555Timer;
100
- `.trim(),
101
- }),
102
- }
103
-
104
- const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
105
-
106
- const response = await axios.get("/api/snippets/get_image", {
107
- params: {
108
- snippetId: createdSnippet.data.snippet.snippet_id,
109
- image_of: "pcb",
110
- format: "svg",
111
- },
112
- })
113
-
114
- expect(response.status).toBe(200)
115
- expect(response.data.ok).toBe(true)
116
- expect(response.data.svg).toContain("<svg")
117
- })
@@ -1,114 +0,0 @@
1
- import { expect, test } from "bun:test"
2
- import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
3
- import { generateCircuitJson } from "bun-tests/fake-snippets-api/fixtures/get-circuit-json"
4
-
5
- test("get schematic svg of a snippet", async () => {
6
- const { axios, db } = await getTestServer()
7
-
8
- const addedSnippet = {
9
- name: "testuser/my-test-board",
10
- unscoped_name: "my-test-board",
11
- owner_name: "testuser",
12
- code: `
13
- import { A555Timer } from "@tsci/seveibar.a555timer"
14
-
15
- export default () => (
16
- <board width="10mm" height="10mm">
17
- <A555Timer name="U1" />
18
- </board>
19
- )`.trim(),
20
- created_at: new Date().toISOString(),
21
- updated_at: new Date().toISOString(),
22
- snippet_type: "board",
23
- description: "A simple board with an A555 Timer component",
24
- circuit_json: await generateCircuitJson({
25
- code: `
26
- import { A555Timer } from "@tsci/seveibar.a555timer"
27
-
28
- export default () => (
29
- <board width="10mm" height="10mm">
30
- <A555Timer name="U1" />
31
- </board>
32
- )`.trim(),
33
- type: "board",
34
- compiled_js: `
35
- "use strict";
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.A555Timer = void 0;
38
- const A555Timer = ({ name }) => /*#__PURE__*/React.createElement("chip", {
39
- name: name,
40
- footprint: "dip8"
41
- });
42
- exports.A555Timer = A555Timer;
43
- `.trim(),
44
- }),
45
- }
46
- const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
47
-
48
- const snippetName = addedSnippet.name.replace(
49
- addedSnippet.owner_name + "/",
50
- "",
51
- )
52
-
53
- const response = await axios.get(
54
- `/api/snippets/images/${createdSnippet.data.snippet.owner_name}/${createdSnippet.data.snippet.unscoped_name}/schematic.svg`,
55
- )
56
-
57
- expect(response.status).toBe(200)
58
- expect(response.data).toContain("<svg")
59
- })
60
-
61
- test("get pcb svg of a snippet", async () => {
62
- const { axios, db } = await getTestServer()
63
-
64
- const addedSnippet = {
65
- name: "testuser/my-test-board",
66
- unscoped_name: "my-test-board",
67
- owner_name: "testuser",
68
- code: `
69
- import { A555Timer } from "@tsci/seveibar.a555timer"
70
-
71
- export default () => (
72
- <board width="10mm" height="10mm">
73
- <A555Timer name="U1" />
74
- </board>
75
- )`.trim(),
76
- created_at: new Date().toISOString(),
77
- updated_at: new Date().toISOString(),
78
- snippet_type: "board",
79
- description: "A simple board with an A555 Timer component",
80
- circuit_json: await generateCircuitJson({
81
- code: `
82
- import { A555Timer } from "@tsci/seveibar.a555timer"
83
-
84
- export default () => (
85
- <board width="10mm" height="10mm">
86
- <A555Timer name="U1" />
87
- </board>
88
- )`.trim(),
89
- type: "board",
90
- compiled_js: `
91
- "use strict";
92
- Object.defineProperty(exports, "__esModule", { value: true });
93
- exports.A555Timer = void 0;
94
- const A555Timer = ({ name }) => /*#__PURE__*/React.createElement("chip", {
95
- name: name,
96
- footprint: "dip8"
97
- });
98
- exports.A555Timer = A555Timer;
99
- `.trim(),
100
- }),
101
- }
102
- const createdSnippet = await axios.post("/api/snippets/create", addedSnippet)
103
-
104
- const snippetName = addedSnippet.name.replace(
105
- addedSnippet.owner_name + "/",
106
- "",
107
- )
108
- const response = await axios.get(
109
- `/api/snippets/images/${createdSnippet.data.snippet.owner_name}/${createdSnippet.data.snippet.unscoped_name}/pcb.svg`,
110
- )
111
-
112
- expect(response.status).toBe(200)
113
- expect(response.data).toContain("<svg")
114
- })
@@ -1,169 +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 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: "testuser",
12
- code: "Content1",
13
- created_at: "2023-01-01T00:00:00Z",
14
- updated_at: "2023-01-01T00:00:00Z",
15
- name: "testuser/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: "testuser",
30
- code: "Content3",
31
- created_at: "2023-01-03T00:00:00Z",
32
- updated_at: "2023-01-03T00:00:00Z",
33
- name: "testuser/Snippet3",
34
- snippet_type: "model",
35
- },
36
- ]
37
-
38
- for (const snippet of snippets) {
39
- db.addSnippet(snippet as any)
40
- }
41
-
42
- // Test without owner_name parameter
43
- const { data: allData } = await axios.get("/api/snippets/list")
44
- expect(allData.snippets).toHaveLength(3)
45
-
46
- // Test with owner_name parameter
47
- const { data: user1Data } = await axios.get("/api/snippets/list", {
48
- params: { owner_name: "testuser" },
49
- })
50
- expect(user1Data.snippets).toHaveLength(2)
51
- expect(
52
- user1Data.snippets.every(
53
- (snippet: { owner_name: string }) => snippet.owner_name === "testuser",
54
- ),
55
- ).toBe(true)
56
-
57
- // Test with non-existent owner
58
- const { data: nonExistentData } = await axios.get("/api/snippets/list", {
59
- params: { owner_name: "NonExistentUser" },
60
- })
61
- expect(nonExistentData.snippets).toHaveLength(0)
62
- })
63
-
64
- test("list snippets by owner", async () => {
65
- const { axios, db } = await getTestServer()
66
-
67
- // Create some test snippets
68
- const snippets = [
69
- {
70
- unscoped_name: "Snippet1",
71
- owner_name: "testuser",
72
- code: "Content1",
73
- created_at: "2023-01-01T00:00:00Z",
74
- updated_at: "2023-01-01T00:00:00Z",
75
- name: "testuser/Snippet1",
76
- snippet_type: "board",
77
- },
78
- {
79
- unscoped_name: "Snippet2",
80
- owner_name: "otheruser",
81
- code: "Content2",
82
- created_at: "2023-01-02T00:00:00Z",
83
- updated_at: "2023-01-02T00:00:00Z",
84
- name: "otheruser/Snippet2",
85
- snippet_type: "package",
86
- },
87
- ]
88
-
89
- for (const snippet of snippets) {
90
- db.addSnippet(snippet as any)
91
- }
92
-
93
- // List snippets by owner
94
- const response = await axios.get("/api/snippets/list?owner_name=testuser")
95
- expect(response.status).toBe(200)
96
- expect(response.data.snippets).toHaveLength(1)
97
- expect(response.data.snippets[0].unscoped_name).toBe("Snippet1")
98
- })
99
-
100
- test("list starred snippets", async () => {
101
- const { axios, db } = await getTestServer()
102
-
103
- // Create some test snippets
104
- const snippets = [
105
- {
106
- unscoped_name: "Snippet1",
107
- code: "Content1",
108
- created_at: "2023-01-01T00:00:00Z",
109
- updated_at: "2023-01-01T00:00:00Z",
110
- name: "testuser/Snippet1",
111
- snippet_type: "board",
112
- },
113
- {
114
- unscoped_name: "Snippet2",
115
- code: "Content2",
116
- created_at: "2023-01-02T00:00:00Z",
117
- updated_at: "2023-01-02T00:00:00Z",
118
- name: "otheruser/Snippet2",
119
- snippet_type: "package",
120
- },
121
- {
122
- unscoped_name: "Snippet3",
123
- code: "Content3",
124
- created_at: "2023-01-03T00:00:00Z",
125
- updated_at: "2023-01-03T00:00:00Z",
126
- name: "thirduser/Snippet3",
127
- snippet_type: "board",
128
- },
129
- ]
130
-
131
- const createdSnippets = []
132
- for (const snippet of snippets) {
133
- const createdSnippet = await axios.post("/api/snippets/create", snippet)
134
- createdSnippets.push(createdSnippet.data.snippet)
135
- }
136
-
137
- // Add stars for testuser
138
- const testUserAccount = db.accounts.find(
139
- (acc) => acc.github_username === "testuser",
140
- )
141
- if (!testUserAccount) throw new Error("testuser account not found")
142
-
143
- // testuser stars Snippet2 and Snippet3
144
- await axios.post("/api/snippets/add_star", {
145
- snippet_id: createdSnippets[1].snippet_id,
146
- })
147
- await axios.post("/api/snippets/add_star", {
148
- snippet_id: createdSnippets[2].snippet_id,
149
- })
150
-
151
- // List starred snippets for testuser
152
- const response = await axios.get("/api/snippets/list?starred_by=testuser", {})
153
- expect(response.status).toBe(200)
154
- expect(response.data.snippets).toHaveLength(2)
155
- expect(
156
- response.data.snippets
157
- .map((s: { unscoped_name: string }) => s.unscoped_name)
158
- .sort(),
159
- ).toEqual(["Snippet2", "Snippet3"])
160
-
161
- // Verify star counts and is_starred flags
162
- for (const snippet of response.data.snippets as Array<{
163
- star_count: number
164
- is_starred: boolean
165
- }>) {
166
- expect(snippet.star_count).toBe(1)
167
- expect(snippet.is_starred).toBe(true)
168
- }
169
- })
@@ -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
- })