-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5036 from systeminit/brit/bug-679-add-api-test-fo…
…r-authoring-flow Add an API test for basic authoring scenario
- Loading branch information
Showing
5 changed files
with
800 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { SdfApiClient } from "./sdf_api_client.ts"; | ||
import assert from "node:assert"; | ||
import { ulid } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
|
||
export function sleep(ms: number) { | ||
const natural_ms = Math.max(0, Math.floor(ms)); | ||
|
@@ -87,3 +89,241 @@ export async function runWithTemporaryChangeset( | |
throw err; | ||
} | ||
} | ||
|
||
export const nilId = "00000000000000000000000000"; | ||
|
||
// Diagram Helpers ------------------------------------------------------------ | ||
|
||
|
||
export async function getQualificationSummary(sdf: SdfApiClient, changeSetId: string) { | ||
return await sdf.call({ | ||
route: "qualification_summary", | ||
routeVars: { | ||
workspaceId: sdf.workspaceId, | ||
changeSetId, | ||
}, | ||
}); | ||
} | ||
|
||
export async function getActions(sdf: SdfApiClient, changeSetId: string) { | ||
return await sdf.call({ | ||
route: "action_list", | ||
routeVars: { | ||
workspaceId: sdf.workspaceId, | ||
changeSetId, | ||
}, | ||
}); | ||
} | ||
|
||
export async function getFuncs(sdf: SdfApiClient, changeSetId: string) { | ||
return await sdf.call({ | ||
route: "func_list", | ||
routeVars: { | ||
workspaceId: sdf.workspaceId, | ||
changeSetId, | ||
}, | ||
}); | ||
} | ||
|
||
|
||
// Prop Helpers ------------------------------------------------------------ | ||
|
||
|
||
export async function getPropertyEditor( | ||
sdf: SdfApiClient, | ||
changeSetId: string, | ||
componentId: string, | ||
) { | ||
const values = await sdf.call({ | ||
route: "get_property_values", | ||
routeVars: { | ||
componentId, | ||
changeSetId, | ||
}, | ||
}); | ||
assert(typeof values?.values === "object", "Expected prop values"); | ||
assert(typeof values?.childValues === "object", "Expected prop childValues:"); | ||
|
||
const schema = await sdf.call({ | ||
route: "get_property_schema", | ||
routeVars: { | ||
componentId, | ||
changeSetId, | ||
}, | ||
}); | ||
assert(typeof schema?.rootPropId === "string", "Expected rootPropId"); | ||
assert(typeof schema?.props === "object", "Expected props"); | ||
assert(typeof schema?.childProps === "object", "Expected childProps list"); | ||
|
||
return { | ||
values, | ||
schema, | ||
}; | ||
} | ||
|
||
export async function setAttributeValue( | ||
sdf: SdfApiClient, | ||
changeSetId: string, | ||
componentId: string, | ||
attributeValueId: string, | ||
parentAttributeValueId: string, | ||
propId: string, | ||
value: unknown, | ||
) { | ||
const updateValuePayload = { | ||
visibility_change_set_pk: changeSetId, | ||
componentId, | ||
attributeValueId, | ||
parentAttributeValueId, | ||
propId, | ||
value, | ||
isForSecret: false, | ||
}; | ||
|
||
await sdf.call({ | ||
route: "update_property_value", | ||
body: updateValuePayload, | ||
}); | ||
} | ||
|
||
export function attributeValueIdForPropPath( | ||
propPath: string, | ||
propList: any[], | ||
attributeValuesView: { | ||
values: any[]; | ||
childValues: any[]; | ||
}, | ||
) { | ||
const prop = propList.find((p) => p.path === propPath); | ||
assert(prop, `Expected to find ${propPath} prop`); | ||
|
||
let attributeValueId; | ||
let value; | ||
for (const attributeValue in attributeValuesView.values) { | ||
if (attributeValuesView.values[attributeValue]?.propId === prop.id) { | ||
attributeValueId = attributeValue; | ||
value = attributeValuesView.values[attributeValue]?.value; | ||
} | ||
} | ||
assert(attributeValueId, "Expected source attribute value"); | ||
|
||
let parentAttributeValueId; | ||
for (const attributeValue in attributeValuesView?.childValues) { | ||
const avChildren = attributeValuesView?.childValues[attributeValue] ?? []; | ||
if (avChildren.includes(attributeValueId)) { | ||
parentAttributeValueId = attributeValue; | ||
} | ||
} | ||
assert(parentAttributeValueId, "Expected parent of source attribute value"); | ||
|
||
return { | ||
attributeValueId, | ||
parentAttributeValueId, | ||
propId: prop.id, | ||
value, | ||
}; | ||
} | ||
|
||
|
||
// Schema Variant Helpers ------------------------------------------------------------ | ||
|
||
export async function createAsset( | ||
sdf: SdfApiClient, | ||
changeSetId: string, | ||
name: string, | ||
): Promise<string> { | ||
const createAssetPayload = { | ||
visibility_change_set_pk: changeSetId, | ||
name, | ||
color: "#AAFF00", | ||
}; | ||
|
||
const createResp = await sdf.call({ route: "create_variant", body: createAssetPayload }); | ||
const schemaVariantId = createResp?.schemaVariantId; | ||
assert(schemaVariantId, "Expected to get a schema variant id after creation"); | ||
return schemaVariantId; | ||
} | ||
|
||
export async function updateAssetCode( | ||
sdf: SdfApiClient, | ||
changeSetId: string, | ||
schemaVariantId: string, | ||
newCode: string, | ||
): Promise<string> { | ||
// Get variant | ||
const variant = await sdf.call({ | ||
route: "get_variant", routeVars: { | ||
workspaceId: sdf.workspaceId, | ||
changeSetId, | ||
schemaVariantId, | ||
}, | ||
}); | ||
// update variant | ||
const updateVariantBody = { | ||
visibility_change_set_pk: changeSetId, | ||
code: newCode, | ||
variant, | ||
}; | ||
|
||
const saveResp = await sdf.call({ | ||
route: "save_variant", | ||
body: updateVariantBody, | ||
}); | ||
const success = saveResp.success; | ||
assert(success, "save was successful"); | ||
|
||
const regenBody = { | ||
visibility_change_set_pk: changeSetId, | ||
variant, | ||
}; | ||
|
||
const regenerateResp = await sdf.call({ | ||
route: "regenerate_variant", | ||
body: regenBody, | ||
}); | ||
|
||
const maybeNewId = regenerateResp.schemaVariantId; | ||
assert(maybeNewId, "Expected to get a schema variant id after regenerate"); | ||
return maybeNewId; | ||
} | ||
|
||
|
||
// Component Helpers ------------------------------------------------------------ | ||
|
||
export async function createComponent( | ||
sdf: SdfApiClient, | ||
changeSetId: string, | ||
schemaVariantId: string, | ||
x: number, | ||
y: number, | ||
parentId?: string, | ||
newCreateComponentApi?: boolean, | ||
): Promise<string> { | ||
const parentArgs = parentId ? { parentId } : {}; | ||
const payload = { | ||
schemaType: newCreateComponentApi ? "installed" : undefined, | ||
schemaVariantId, | ||
x: x.toString(), | ||
y: y.toString(), | ||
visibility_change_set_pk: changeSetId, | ||
workspaceId: sdf.workspaceId, | ||
...parentArgs, | ||
}; | ||
const createResp = await sdf.call({ | ||
route: "create_component", | ||
body: payload, | ||
}); | ||
const componentId = createResp?.componentId; | ||
assert(componentId, "Expected to get a component id after creation"); | ||
|
||
// Run side effect calls | ||
await Promise.all([ | ||
getQualificationSummary(sdf, changeSetId), | ||
getActions(sdf, changeSetId), | ||
getFuncs(sdf, changeSetId), | ||
getPropertyEditor(sdf, changeSetId, componentId), | ||
]); | ||
|
||
return componentId; | ||
} | ||
|
Oops, something went wrong.