Merge branch 'main' into hosted

This commit is contained in:
Abi Raja 2023-12-08 16:48:47 -05:00
commit 7155a0616e
3 changed files with 37 additions and 12 deletions

View File

@ -216,10 +216,17 @@ function App() {
return;
}
const updatedHistory = [
...extractHistoryTree(appHistory, currentVersion),
updateInstruction,
];
let historyTree;
try {
historyTree = extractHistoryTree(appHistory, currentVersion);
} catch {
toast.error(
"Version history is invalid. This shouldn't happen. Please contact support or open a Github issue."
);
return;
}
const updatedHistory = [...historyTree, updateInstruction];
if (shouldIncludeResultImage) {
const resultImage = await takeScreenshot();

View File

@ -53,7 +53,26 @@ const longerBranchingHistory: History = [
},
];
test("should only include history from this point onward", () => {
const basicBadHistory: History = [
{
type: "ai_create",
parentIndex: null,
code: "<html>1. create</html>",
inputs: {
image_url: "",
},
},
{
type: "ai_edit",
parentIndex: 2, // <- Bad parent index
code: "<html>2. edit with better icons</html>",
inputs: {
prompt: "use better icons",
},
},
];
test("should correctly extract the history tree", () => {
expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([
"<html>1. create</html>",
"use better icons",
@ -93,11 +112,12 @@ test("should only include history from this point onward", () => {
"<html>3. edit with better icons and red text</html>",
]);
// Errors - TODO: Handle these
// Errors
// Bad index
// TODO: Throw an exception instead?
expect(extractHistoryTree(basicLinearHistory, 100)).toEqual([]);
expect(extractHistoryTree(basicLinearHistory, -2)).toEqual([]);
expect(() => extractHistoryTree(basicLinearHistory, 100)).toThrow();
expect(() => extractHistoryTree(basicLinearHistory, -2)).toThrow();
// Bad tree
expect(() => extractHistoryTree(basicBadHistory, 1)).toThrow();
});

View File

@ -22,9 +22,7 @@ export function extractHistoryTree(
// Move to the parent of the current item
currentIndex = item.parentIndex;
} else {
// TODO: Throw an exception here?
// Break the loop if the item is not found (should not happen in a well-formed history)
break;
throw new Error("Malformed history: missing parent index");
}
}