From b4a9bbd9da2b267ae583f12ed51f5129dab537a0 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Sat, 31 Aug 2024 17:22:18 +0200 Subject: [PATCH] fix unit tests --- frontend/src/components/history/utils.test.ts | 441 +++++++++--------- 1 file changed, 226 insertions(+), 215 deletions(-) diff --git a/frontend/src/components/history/utils.test.ts b/frontend/src/components/history/utils.test.ts index f70d2bc..7a5aaa2 100644 --- a/frontend/src/components/history/utils.test.ts +++ b/frontend/src/components/history/utils.test.ts @@ -1,231 +1,242 @@ -// import { extractHistoryTree, renderHistory } from "./utils"; -// import type { History } from "./history_types"; +import { extractHistory, renderHistory } from "./utils"; +import { Commit, CommitHash } from "../commits/types"; -// const basicLinearHistory: History = [ -// { -// type: "ai_create", -// parentIndex: null, -// code: "1. create", -// inputs: { -// image_url: "", -// }, -// }, -// { -// type: "ai_edit", -// parentIndex: 0, -// code: "2. edit with better icons", -// inputs: { -// prompt: "use better icons", -// }, -// }, -// { -// type: "ai_edit", -// parentIndex: 1, -// code: "3. edit with better icons and red text", -// inputs: { -// prompt: "make text red", -// }, -// }, -// ]; +const basicLinearHistory: Record = { + "0": { + hash: "0", + dateCreated: new Date(), + isCommitted: false, + type: "ai_create", + parentHash: null, + variants: [{ code: "1. create" }], + selectedVariantIndex: 0, + inputs: { + image_url: "", + }, + }, + "1": { + hash: "1", + dateCreated: new Date(), + isCommitted: false, + type: "ai_edit", + parentHash: "0", + variants: [{ code: "2. edit with better icons" }], + selectedVariantIndex: 0, + inputs: { + prompt: "use better icons", + }, + }, + "2": { + hash: "2", + dateCreated: new Date(), + isCommitted: false, + type: "ai_edit", + parentHash: "1", + variants: [{ code: "3. edit with better icons and red text" }], + selectedVariantIndex: 0, + inputs: { + prompt: "make text red", + }, + }, +}; -// const basicLinearHistoryWithCode: History = [ -// { -// type: "code_create", -// parentIndex: null, -// code: "1. create", -// inputs: { -// code: "1. create", -// }, -// }, -// ...basicLinearHistory.slice(1), -// ]; +const basicLinearHistoryWithCode: Record = { + "0": { + hash: "0", + dateCreated: new Date(), + isCommitted: false, + type: "code_create", + parentHash: null, + variants: [{ code: "1. create" }], + selectedVariantIndex: 0, + inputs: null, + }, + ...Object.fromEntries(Object.entries(basicLinearHistory).slice(1)), +}; -// const basicBranchingHistory: History = [ -// ...basicLinearHistory, -// { -// type: "ai_edit", -// parentIndex: 1, -// code: "4. edit with better icons and green text", -// inputs: { -// prompt: "make text green", -// }, -// }, -// ]; +const basicBranchingHistory: Record = { + ...basicLinearHistory, + "3": { + hash: "3", + dateCreated: new Date(), + isCommitted: false, + type: "ai_edit", + parentHash: "1", + variants: [ + { code: "4. edit with better icons and green text" }, + ], + selectedVariantIndex: 0, + inputs: { + prompt: "make text green", + }, + }, +}; -// const longerBranchingHistory: History = [ -// ...basicBranchingHistory, -// { -// type: "ai_edit", -// parentIndex: 3, -// code: "5. edit with better icons and green, bold text", -// inputs: { -// prompt: "make text bold", -// }, -// }, -// ]; +const longerBranchingHistory: Record = { + ...basicBranchingHistory, + "4": { + hash: "4", + dateCreated: new Date(), + isCommitted: false, + type: "ai_edit", + parentHash: "3", + variants: [ + { code: "5. edit with better icons and green, bold text" }, + ], + selectedVariantIndex: 0, + inputs: { + prompt: "make text bold", + }, + }, +}; -// const basicBadHistory: History = [ -// { -// type: "ai_create", -// parentIndex: null, -// code: "1. create", -// inputs: { -// image_url: "", -// }, -// }, -// { -// type: "ai_edit", -// parentIndex: 2, // <- Bad parent index -// code: "2. edit with better icons", -// inputs: { -// prompt: "use better icons", -// }, -// }, -// ]; +const basicBadHistory: Record = { + "0": { + hash: "0", + dateCreated: new Date(), + isCommitted: false, + type: "ai_create", + parentHash: null, + variants: [{ code: "1. create" }], + selectedVariantIndex: 0, + inputs: { + image_url: "", + }, + }, + "1": { + hash: "1", + dateCreated: new Date(), + isCommitted: false, + type: "ai_edit", + parentHash: "2", // <- Bad parent hash + variants: [{ code: "2. edit with better icons" }], + selectedVariantIndex: 0, + inputs: { + prompt: "use better icons", + }, + }, +}; -// describe("History Utils", () => { -// test("should correctly extract the history tree", () => { -// expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([ -// "1. create", -// "use better icons", -// "2. edit with better icons", -// "make text red", -// "3. edit with better icons and red text", -// ]); +describe("History Utils", () => { + test("should correctly extract the history tree", () => { + expect(extractHistory("2", basicLinearHistory)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text red", + "3. edit with better icons and red text", + ]); -// expect(extractHistoryTree(basicLinearHistory, 0)).toEqual([ -// "1. create", -// ]); + expect(extractHistory("0", basicLinearHistory)).toEqual([ + "1. create", + ]); -// // Test branching -// expect(extractHistoryTree(basicBranchingHistory, 3)).toEqual([ -// "1. create", -// "use better icons", -// "2. edit with better icons", -// "make text green", -// "4. edit with better icons and green text", -// ]); + // Test branching + expect(extractHistory("3", basicBranchingHistory)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text green", + "4. edit with better icons and green text", + ]); -// expect(extractHistoryTree(longerBranchingHistory, 4)).toEqual([ -// "1. create", -// "use better icons", -// "2. edit with better icons", -// "make text green", -// "4. edit with better icons and green text", -// "make text bold", -// "5. edit with better icons and green, bold text", -// ]); + expect(extractHistory("4", longerBranchingHistory)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text green", + "4. edit with better icons and green text", + "make text bold", + "5. edit with better icons and green, bold text", + ]); -// expect(extractHistoryTree(longerBranchingHistory, 2)).toEqual([ -// "1. create", -// "use better icons", -// "2. edit with better icons", -// "make text red", -// "3. edit with better icons and red text", -// ]); + expect(extractHistory("2", longerBranchingHistory)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text red", + "3. edit with better icons and red text", + ]); -// // Errors + // Errors -// // Bad index -// expect(() => extractHistoryTree(basicLinearHistory, 100)).toThrow(); -// expect(() => extractHistoryTree(basicLinearHistory, -2)).toThrow(); + // Bad hash + expect(() => extractHistory("100", basicLinearHistory)).toThrow(); -// // Bad tree -// expect(() => extractHistoryTree(basicBadHistory, 1)).toThrow(); -// }); + // Bad tree + expect(() => extractHistory("1", basicBadHistory)).toThrow(); + }); -// test("should correctly render the history tree", () => { -// expect(renderHistory(basicLinearHistory, 2)).toEqual([ -// { -// isActive: false, -// parentVersion: null, -// summary: "Create", -// type: "Create", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "use better icons", -// type: "Edit", -// }, -// { -// isActive: true, -// parentVersion: null, -// summary: "make text red", -// type: "Edit", -// }, -// ]); + test("should correctly render the history tree", () => { + expect(renderHistory(Object.values(basicLinearHistory))).toEqual([ + { + ...basicLinearHistory["0"], + type: "Create", + summary: "Create", + parentVersion: null, + }, + { + ...basicLinearHistory["1"], + type: "Edit", + summary: "use better icons", + parentVersion: null, + }, + { + ...basicLinearHistory["2"], + type: "Edit", + summary: "make text red", + parentVersion: null, + }, + ]); -// // Current version is the first version -// expect(renderHistory(basicLinearHistory, 0)).toEqual([ -// { -// isActive: true, -// parentVersion: null, -// summary: "Create", -// type: "Create", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "use better icons", -// type: "Edit", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "make text red", -// type: "Edit", -// }, -// ]); + // Render a history with code + expect(renderHistory(Object.values(basicLinearHistoryWithCode))).toEqual([ + { + ...basicLinearHistoryWithCode["0"], + type: "Imported from code", + summary: "Imported from code", + parentVersion: null, + }, + { + ...basicLinearHistoryWithCode["1"], + type: "Edit", + summary: "use better icons", + parentVersion: null, + }, + { + ...basicLinearHistoryWithCode["2"], + type: "Edit", + summary: "make text red", + parentVersion: null, + }, + ]); -// // Render a history with code -// expect(renderHistory(basicLinearHistoryWithCode, 0)).toEqual([ -// { -// isActive: true, -// parentVersion: null, -// summary: "Imported from code", -// type: "Imported from code", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "use better icons", -// type: "Edit", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "make text red", -// type: "Edit", -// }, -// ]); - -// // Render a non-linear history -// expect(renderHistory(basicBranchingHistory, 3)).toEqual([ -// { -// isActive: false, -// parentVersion: null, -// summary: "Create", -// type: "Create", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "use better icons", -// type: "Edit", -// }, -// { -// isActive: false, -// parentVersion: null, -// summary: "make text red", -// type: "Edit", -// }, -// { -// isActive: true, -// parentVersion: "v2", -// summary: "make text green", -// type: "Edit", -// }, -// ]); -// }); -// }); + // Render a non-linear history + expect(renderHistory(Object.values(basicBranchingHistory))).toEqual([ + { + ...basicBranchingHistory["0"], + type: "Create", + summary: "Create", + parentVersion: null, + }, + { + ...basicBranchingHistory["1"], + type: "Edit", + summary: "use better icons", + parentVersion: null, + }, + { + ...basicBranchingHistory["2"], + type: "Edit", + summary: "make text red", + parentVersion: null, + }, + { + ...basicBranchingHistory["3"], + type: "Edit", + summary: "make text green", + parentVersion: 2, + }, + ]); + }); +});