@tscircuit/fake-snippets 0.0.8 → 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 (34) hide show
  1. package/bun-tests/fake-snippets-api/routes/packages/list-1.test.ts +0 -3
  2. package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +32 -27
  3. package/bun-tests/fake-snippets-api/routes/snippets/get.test.ts +114 -0
  4. package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +10 -6
  5. package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +8 -6
  6. package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +2 -2
  7. package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +10 -10
  8. package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +8 -6
  9. package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +1 -1
  10. package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +19 -12
  11. package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +57 -16
  12. package/bun.lock +145 -569
  13. package/dist/bundle.js +724 -158
  14. package/fake-snippets-api/lib/db/db-client.ts +727 -108
  15. package/fake-snippets-api/lib/db/schema.ts +12 -0
  16. package/fake-snippets-api/lib/public-mapping/public-map-package.ts +2 -1
  17. package/fake-snippets-api/routes/api/packages/list.ts +4 -1
  18. package/fake-snippets-api/routes/api/snippets/add_star.ts +30 -8
  19. package/fake-snippets-api/routes/api/snippets/create.ts +16 -16
  20. package/fake-snippets-api/routes/api/snippets/delete.ts +5 -5
  21. package/fake-snippets-api/routes/api/snippets/download.ts +24 -10
  22. package/fake-snippets-api/routes/api/snippets/get.ts +46 -13
  23. package/fake-snippets-api/routes/api/snippets/list.ts +37 -2
  24. package/fake-snippets-api/routes/api/snippets/update.ts +36 -14
  25. package/package.json +3 -5
  26. package/src/components/CodeAndPreview.tsx +13 -48
  27. package/src/components/CodeEditor.tsx +10 -7
  28. package/src/components/EditorNav.tsx +0 -21
  29. package/src/components/PreviewContent.tsx +2 -2
  30. package/src/components/ViewSnippetHeader.tsx +4 -0
  31. package/src/hooks/use-global-store.ts +0 -5
  32. package/src/hooks/use-package-as-snippet.ts +78 -0
  33. package/src/hooks/use-run-tsx/index.tsx +4 -0
  34. 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
  {
@@ -1,27 +1,27 @@
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
4
  test("add star to snippet", async () => {
5
- const { axios, db } = await getTestServer()
5
+ const { axios } = await getTestServer()
6
6
 
7
- // Add a test snippet
8
- const snippet = {
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 addedSnippet = db.addSnippet(snippet as any)!
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: addedSnippet.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 in database
40
- expect(db.hasStarred("account-1234", addedSnippet.snippet_id)).toBe(true)
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, db } = await getTestServer()
71
+ const { axios } = await getTestServer()
67
72
 
68
- // Add a test snippet
69
- const snippet = {
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 addedSnippet = db.addSnippet(snippet as any)
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: addedSnippet.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: addedSnippet.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 = db.addSnippet({
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: addedSnippet.snippet_id,
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 = db.addSnippet({
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: addedSnippet.snippet_id,
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 = db.addSnippet({
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/${addedSnippet.owner_name}/${snippetName}/schematic.svg`,
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 = db.addSnippet({
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/${addedSnippet.owner_name}/${snippetName}/pcb.svg`,
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("Snippet3")
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("Snippet1")
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
- db.addSnippet(snippet as any)
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 snippet1 = db.snippets[0]
48
- const snippet2 = db.snippets[1]
49
- const snippet3 = db.snippets[2]
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", snippet2.snippet_id)
53
- db.addStar("user2", snippet2.snippet_id)
54
- db.addStar("user3", snippet2.snippet_id)
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", snippet3.snippet_id)
58
- db.addStar("user2", snippet3.snippet_id)
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", snippet1.snippet_id)
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: addedSnippet.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: addedSnippet.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(db.hasStarred("account-123", addedSnippet.snippet_id)).toBe(false)
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 addedSnippet = db.addSnippet(snippet as any)
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: addedSnippet.snippet_id,
99
+ snippet_id: createdSnippet.data.snippet.snippet_id,
98
100
  },
99
101
  {
100
102
  headers: {
@@ -1,5 +1,5 @@
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
4
  test("search snippets", async () => {
5
5
  const { axios, db } = await getTestServer()
@@ -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
  })