|
| 1 | +import { env } from "cloudflare:workers"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { createTestApp, jsonRequest } from "../../../__test_utils__"; |
| 4 | +import { type ElementPath } from "../../lib/onshape/path"; |
| 5 | +import * as AssemblyEndpoints from "../../lib/onshape/endpoints/assemblies"; |
| 6 | +import * as DocumentEndpoints from "../../lib/onshape/endpoints/documents"; |
| 7 | +import { OnshapeElementType } from "../../lib/onshape/endpoints/documents"; |
| 8 | +import type { BoltHelperResult, EdgeSelection } from "./contract"; |
| 9 | + |
| 10 | +/** Features are only editable in a workspace, so the target is one. */ |
| 11 | +const TARGET_PATH: ElementPath = { |
| 12 | + documentId: "doc-test", |
| 13 | + instanceId: "w-test", |
| 14 | + instanceType: "w", |
| 15 | + elementId: "e-assembly" |
| 16 | +}; |
| 17 | + |
| 18 | +const EDGES: EdgeSelection[] = [ |
| 19 | + { selectionId: "JH1", occurrencePath: ["M1"] }, |
| 20 | + { selectionId: "JH2", occurrencePath: [] } |
| 21 | +]; |
| 22 | + |
| 23 | +function mockElement(elementType: OnshapeElementType) { |
| 24 | + return vi |
| 25 | + .spyOn(DocumentEndpoints, "getDocumentElement") |
| 26 | + .mockResolvedValue({ name: "Assembly 1", elementType }); |
| 27 | +} |
| 28 | + |
| 29 | +function postBoltHelper(body: unknown, signedIn = true) { |
| 30 | + return createTestApp({ signedIn }).request( |
| 31 | + "/api/bolt-helper", |
| 32 | + jsonRequest("POST", body), |
| 33 | + env |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +describe("POST /bolt-helper", () => { |
| 38 | + afterEach(() => vi.restoreAllMocks()); |
| 39 | + |
| 40 | + it("adds a fasten mate per edge, mated to that edge's center", async () => { |
| 41 | + mockElement(OnshapeElementType.ASSEMBLY); |
| 42 | + const addFeature = vi |
| 43 | + .spyOn(AssemblyEndpoints, "addAssemblyFeature") |
| 44 | + .mockResolvedValueOnce({ feature: { featureId: "f1" } }) |
| 45 | + .mockResolvedValueOnce({ feature: { featureId: "f2" } }); |
| 46 | + |
| 47 | + const res = await postBoltHelper({ |
| 48 | + targetPath: TARGET_PATH, |
| 49 | + edges: EDGES |
| 50 | + }); |
| 51 | + expect(res.status).toBe(200); |
| 52 | + |
| 53 | + const body: BoltHelperResult = await res.json(); |
| 54 | + expect(body.featureIds).toEqual(["f1", "f2"]); |
| 55 | + expect(body.elementName).toBe("Assembly 1"); |
| 56 | + expect(addFeature).toHaveBeenCalledTimes(2); |
| 57 | + |
| 58 | + const feature = JSON.stringify(addFeature.mock.calls[0][2]); |
| 59 | + expect(feature).toContain('qTransient(\\"JH1\\")'); |
| 60 | + expect(feature).toContain("CENTER"); |
| 61 | + expect(feature).toContain("M1"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("rejects a tab that is not an assembly", async () => { |
| 65 | + mockElement(OnshapeElementType.PART_STUDIO); |
| 66 | + |
| 67 | + const res = await postBoltHelper({ |
| 68 | + targetPath: TARGET_PATH, |
| 69 | + edges: EDGES |
| 70 | + }); |
| 71 | + expect(res.status).toBe(400); |
| 72 | + }); |
| 73 | + |
| 74 | + it("rejects a version, which has no editable feature list", async () => { |
| 75 | + const res = await postBoltHelper({ |
| 76 | + targetPath: { ...TARGET_PATH, instanceType: "v" }, |
| 77 | + edges: EDGES |
| 78 | + }); |
| 79 | + expect(res.status).toBe(400); |
| 80 | + }); |
| 81 | + |
| 82 | + it("rejects a request with no edges selected", async () => { |
| 83 | + const res = await postBoltHelper({ |
| 84 | + targetPath: TARGET_PATH, |
| 85 | + edges: [] |
| 86 | + }); |
| 87 | + expect(res.status).toBe(400); |
| 88 | + }); |
| 89 | + |
| 90 | + it("requires a signed-in caller", async () => { |
| 91 | + const res = await postBoltHelper( |
| 92 | + { targetPath: TARGET_PATH, edges: EDGES }, |
| 93 | + false |
| 94 | + ); |
| 95 | + expect(res.status).toBe(401); |
| 96 | + }); |
| 97 | +}); |
0 commit comments