@tscircuit/fake-snippets 0.0.7 → 0.0.9

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 (37) hide show
  1. package/bun-tests/fake-snippets-api/fixtures/get-test-server.ts +2 -5
  2. package/bun-tests/fake-snippets-api/routes/packages/{list.test.ts → list-1.test.ts} +0 -55
  3. package/bun-tests/fake-snippets-api/routes/packages/list-2.test.ts +59 -0
  4. package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +32 -27
  5. package/bun-tests/fake-snippets-api/routes/snippets/create.test.ts +34 -1
  6. package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +114 -0
  7. package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +10 -6
  8. package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +8 -6
  9. package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +2 -2
  10. package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +10 -10
  11. package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +8 -6
  12. package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +1 -1
  13. package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +19 -12
  14. package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +57 -16
  15. package/bun.lock +148 -569
  16. package/dist/bundle.js +844 -194
  17. package/fake-snippets-api/lib/db/db-client.ts +761 -147
  18. package/fake-snippets-api/lib/db/schema.ts +27 -6
  19. package/fake-snippets-api/lib/public-mapping/public-map-package.ts +8 -0
  20. package/fake-snippets-api/routes/api/packages/list.ts +4 -1
  21. package/fake-snippets-api/routes/api/snippets/add_star.ts +30 -8
  22. package/fake-snippets-api/routes/api/snippets/create.ts +123 -29
  23. package/fake-snippets-api/routes/api/snippets/delete.ts +5 -5
  24. package/fake-snippets-api/routes/api/snippets/download.ts +24 -10
  25. package/fake-snippets-api/routes/api/snippets/get.ts +46 -13
  26. package/fake-snippets-api/routes/api/snippets/list.ts +37 -2
  27. package/fake-snippets-api/routes/api/snippets/update.ts +36 -14
  28. package/package.json +4 -5
  29. package/src/components/CodeAndPreview.tsx +13 -48
  30. package/src/components/CodeEditor.tsx +10 -7
  31. package/src/components/EditorNav.tsx +0 -21
  32. package/src/components/PreviewContent.tsx +2 -2
  33. package/src/components/ViewSnippetHeader.tsx +4 -0
  34. package/src/hooks/use-global-store.ts +0 -5
  35. package/src/hooks/use-package-as-snippet.ts +78 -0
  36. package/src/hooks/use-run-tsx/index.tsx +4 -0
  37. package/src/lib/jlc-parts-engine.ts +4 -2
@@ -1,10 +1,10 @@
1
1
  import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
2
- import { test, expect } from "bun:test"
2
+ import { expect, test } from "bun:test"
3
3
 
4
- test("star count is correctly calculated", async () => {
4
+ test("star count is updated correctly", async () => {
5
5
  const { axios, db } = await getTestServer()
6
6
 
7
- // Add a test snippet
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 addedSnippet = db.addSnippet(snippet as any)
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
- // Add some stars from different users
21
- db.addStar("user1", addedSnippet.snippet_id)
22
- db.addStar("user2", addedSnippet.snippet_id)
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: addedSnippet.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
- // Remove a star
38
- db.removeStar("user2", addedSnippet.snippet_id)
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(2)
50
+ expect(updatedListResponse.data.snippets[0].star_count).toBe(3)
44
51
  })
@@ -7,7 +7,7 @@ test("update snippet", async () => {
7
7
  // Add a test snippet
8
8
  const snippet = {
9
9
  unscoped_name: "TestSnippet",
10
- owner_name: "testuser",
10
+ owner_name: "account-1234",
11
11
  code: "Original Content",
12
12
  created_at: "2023-01-01T00:00:00Z",
13
13
  updated_at: "2023-01-01T00:00:00Z",
@@ -18,7 +18,7 @@ test("update snippet", async () => {
18
18
  }
19
19
  db.addSnippet(snippet as any)
20
20
 
21
- const addedSnippet = db.snippets[0]
21
+ const addedPackage = db.packages[0]
22
22
 
23
23
  // Update the snippet
24
24
  const updatedCode = "Updated Content"
@@ -26,7 +26,7 @@ test("update snippet", async () => {
26
26
  const response = await axios.post(
27
27
  "/api/snippets/update",
28
28
  {
29
- snippet_id: addedSnippet.snippet_id,
29
+ snippet_id: addedPackage.package_id,
30
30
  code: updatedCode,
31
31
  compiled_js: updatedCompiledJs,
32
32
  },
@@ -40,13 +40,16 @@ test("update snippet", async () => {
40
40
  expect(response.status).toBe(200)
41
41
  expect(response.data.snippet.code).toBe(updatedCode)
42
42
  expect(response.data.snippet.compiled_js).toBe(updatedCompiledJs)
43
- expect(response.data.snippet.updated_at).not.toBe(addedSnippet.created_at)
43
+ expect(response.data.snippet.updated_at).not.toBe(addedPackage.created_at)
44
44
 
45
45
  // Verify the snippet was updated in the database
46
- const updatedSnippet = db.snippets[0]
47
- expect(updatedSnippet.code).toBe(updatedCode)
48
- expect(updatedSnippet.compiled_js).toBe(updatedCompiledJs)
49
- expect(updatedSnippet.updated_at).not.toBe(updatedSnippet.created_at)
46
+ const updatedPackageFiles = db.packageFiles.filter(
47
+ (p) => p.package_release_id === addedPackage.latest_package_release_id,
48
+ )
49
+ expect(updatedPackageFiles.length).toBe(3)
50
+ expect(updatedPackageFiles[0].content_text).toBe(updatedCode)
51
+ expect(updatedPackageFiles[1].content_text).toBe("") // dts
52
+ expect(updatedPackageFiles[2].content_text).toBe(updatedCompiledJs)
50
53
  })
51
54
 
52
55
  test("update non-existent snippet", async () => {
@@ -80,7 +83,7 @@ test("update snippet with null compiled_js", async () => {
80
83
  // Add a test snippet with compiled_js
81
84
  const snippet = {
82
85
  unscoped_name: "TestSnippet",
83
- owner_name: "testuser",
86
+ owner_name: "account-1234",
84
87
  code: "Original Content",
85
88
  created_at: "2023-01-01T00:00:00Z",
86
89
  updated_at: "2023-01-01T00:00:00Z",
@@ -89,28 +92,66 @@ test("update snippet with null compiled_js", async () => {
89
92
  description: "Original Description",
90
93
  compiled_js: "console.log('Original Content')",
91
94
  }
95
+
92
96
  db.addSnippet(snippet as any)
93
97
 
94
- const addedSnippet = db.snippets[0]
98
+ const addedPackage = db.packages[0]
95
99
 
96
100
  // Update the snippet with null compiled_js
97
101
  const response = await axios.post(
98
102
  "/api/snippets/update",
99
103
  {
100
- snippet_id: addedSnippet.snippet_id,
101
- compiled_js: null,
104
+ snippet_id: addedPackage.package_id,
105
+ compiled_js: "",
102
106
  },
103
107
  {
104
108
  headers: {
105
- Authorization: "Bearer 1234",
109
+ Authorization: `Bearer ${addedPackage.creator_account_id}`,
106
110
  },
107
111
  },
108
112
  )
109
113
 
110
114
  expect(response.status).toBe(200)
111
- expect(response.data.snippet.compiled_js).toBeNull()
115
+ expect(response.data.snippet.compiled_js).toBeEmpty()
112
116
 
113
117
  // Verify the snippet was updated in the database
114
- const updatedSnippet = db.snippets[0]
115
- expect(updatedSnippet.compiled_js).toBeNull()
118
+ const updatedPackageFiles = db.packageFiles.filter(
119
+ (p) => p.package_release_id === addedPackage.latest_package_release_id,
120
+ )
121
+ expect(updatedPackageFiles.length).toBe(3)
122
+ expect(updatedPackageFiles[0].content_text).toBe(snippet.code)
123
+ expect(updatedPackageFiles[1].content_text).toBe("")
124
+ })
125
+
126
+ test("update snippet after create snippet", async () => {
127
+ const { axios, db } = await getTestServer()
128
+
129
+ const snippet = {
130
+ unscoped_name: "TestSnippet",
131
+ code: "Test Content",
132
+ snippet_type: "package",
133
+ description: "Test Description",
134
+ }
135
+
136
+ await axios.post("/api/snippets/create", snippet)
137
+
138
+ const createdSnippet = db.packages[0]
139
+
140
+ const updatedCode = "Updated Content"
141
+ const response = await axios.post(
142
+ "/api/snippets/update",
143
+ {
144
+ snippet_id: createdSnippet.package_id,
145
+ code: updatedCode,
146
+ },
147
+ {
148
+ headers: {
149
+ Authorization: `Bearer ${createdSnippet.creator_account_id}`,
150
+ },
151
+ },
152
+ )
153
+
154
+ expect(response.status).toBe(200)
155
+ expect(response.data.snippet.code).toBe(updatedCode)
156
+ expect(response.data.snippet.updated_at).not.toBe(createdSnippet.created_at)
116
157
  })