Skip to content

Commit f3e6136

Browse files
authored
Add fake for /packages/images with the fs_sha param (#1066)
* Add fake for `/packages/images` with the `fs_sha` param * format
1 parent 31a22c0 commit f3e6136

6 files changed

Lines changed: 225 additions & 8 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { getTestServer } from "bun-tests/fake-snippets-api/fixtures/get-test-server"
2+
import { expect, test } from "bun:test"
3+
import { generateCircuitJson } from "bun-tests/fake-snippets-api/fixtures/get-circuit-json"
4+
import md5 from "md5"
5+
test("get schematic svg of a package", async () => {
6+
const { axios, db } = await getTestServer()
7+
8+
// create a package
9+
const pkg = await axios.post("/api/packages/create", {
10+
name: "testuser/TestPackage",
11+
description: "Test Description",
12+
})
13+
14+
// create a package release
15+
const pkg_release = await axios.post("/api/package_releases/create", {
16+
package_id: pkg.data.package.package_id,
17+
version: "1.0.0",
18+
is_latest: true,
19+
})
20+
21+
const circuit_json = await generateCircuitJson({
22+
code: `
23+
import { A555Timer } from "@tsci/seveibar.a555timer"
24+
25+
export default () => (
26+
<board width="10mm" height="10mm">
27+
<A555Timer name="U1" />
28+
</board>
29+
)`.trim(),
30+
type: "board",
31+
compiled_js: `
32+
"use strict";
33+
Object.defineProperty(exports, "__esModule", { value: true });
34+
exports.A555Timer = void 0;
35+
const A555Timer = ({ name }) => /*#__PURE__*/React.createElement("chip", {
36+
name: name,
37+
footprint: "dip8"
38+
});
39+
exports.A555Timer = A555Timer;
40+
`.trim(),
41+
})
42+
// create a package file
43+
const pkg_file = await axios.post("/api/package_files/create", {
44+
package_release_id: pkg_release.data.package_release.package_release_id,
45+
file_path: "circuit.json",
46+
content_text: JSON.stringify(circuit_json),
47+
})
48+
49+
// create fsMap by getting all the files in the package with the file_path and content_text
50+
const fsMap = new Map<string, string>()
51+
// list all the files in the package
52+
const files = await axios.post("/api/package_files/list", {
53+
package_release_id: pkg_release.data.package_release.package_release_id,
54+
})
55+
for (const file of files.data.package_files) {
56+
// get the file content
57+
const file_content = await axios.post("/api/package_files/get", {
58+
package_file_id: file.package_file_id,
59+
})
60+
fsMap.set(file.file_path, file_content.data.package_file.content_text)
61+
}
62+
63+
const fsMapHash = md5(JSON.stringify(fsMap))
64+
65+
// update the package release with the fsMapHash
66+
await axios.post("/api/package_releases/update", {
67+
package_release_id: pkg_release.data.package_release.package_release_id,
68+
fs_sha: `md5-${fsMapHash}`,
69+
})
70+
71+
const response = await axios.get(
72+
`/api/packages/images/${pkg.data.package.owner_github_username}/${pkg.data.package.unscoped_name}/schematic.svg`,
73+
{
74+
params: {
75+
fs_sha: `md5-${fsMapHash}`,
76+
},
77+
},
78+
)
79+
80+
expect(response.status).toBe(200)
81+
expect(response.data).toContain("<svg")
82+
})

bun.lock

Lines changed: 18 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fake-snippets-api/lib/db/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export const packageReleaseSchema = z.object({
175175
.optional(),
176176
has_transpiled: z.boolean().default(false),
177177
transpilation_error: z.string().nullable().optional(),
178+
fs_sha: z.string().nullable().optional(),
178179
})
179180
export type PackageRelease = z.infer<typeof packageReleaseSchema>
180181

fake-snippets-api/routes/api/package_releases/update.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default withRouteSpec({
1010
is_locked: z.boolean().optional(),
1111
is_latest: z.boolean().optional(),
1212
license: z.string().optional(),
13+
fs_sha: z.string().optional(),
1314
}),
1415
jsonResponse: z.object({
1516
ok: z.boolean(),
@@ -21,6 +22,7 @@ export default withRouteSpec({
2122
is_locked,
2223
is_latest,
2324
license,
25+
fs_sha,
2426
} = req.jsonBody
2527
let releaseId = package_release_id
2628

@@ -45,7 +47,7 @@ export default withRouteSpec({
4547
})
4648
}
4749

48-
const delta = { is_locked, is_latest, license }
50+
const delta = { is_locked, is_latest, license, fs_sha }
4951
if (
5052
Object.keys(delta).filter(
5153
(k) => delta[k as keyof typeof delta] !== undefined,
@@ -73,6 +75,7 @@ export default withRouteSpec({
7375
...(is_locked !== undefined && { is_locked }),
7476
...(is_latest !== undefined && { is_latest }),
7577
...(license !== undefined && { license }),
78+
...(fs_sha !== undefined && { fs_sha }),
7679
}
7780

7881
// Handle is_latest updates atomically

0 commit comments

Comments
 (0)