fix unit tests

This commit is contained in:
Abi Raja 2024-08-31 17:22:18 +02:00
parent 0db8a206f2
commit b4a9bbd9da

View File

@ -1,231 +1,242 @@
// import { extractHistoryTree, renderHistory } from "./utils"; import { extractHistory, renderHistory } from "./utils";
// import type { History } from "./history_types"; import { Commit, CommitHash } from "../commits/types";
// const basicLinearHistory: History = [ const basicLinearHistory: Record<CommitHash, Commit> = {
// { "0": {
// type: "ai_create", hash: "0",
// parentIndex: null, dateCreated: new Date(),
// code: "<html>1. create</html>", isCommitted: false,
// inputs: { type: "ai_create",
// image_url: "", parentHash: null,
// }, variants: [{ code: "<html>1. create</html>" }],
// }, selectedVariantIndex: 0,
// { inputs: {
// type: "ai_edit", image_url: "",
// parentIndex: 0, },
// code: "<html>2. edit with better icons</html>", },
// inputs: { "1": {
// prompt: "use better icons", hash: "1",
// }, dateCreated: new Date(),
// }, isCommitted: false,
// { type: "ai_edit",
// type: "ai_edit", parentHash: "0",
// parentIndex: 1, variants: [{ code: "<html>2. edit with better icons</html>" }],
// code: "<html>3. edit with better icons and red text</html>", selectedVariantIndex: 0,
// inputs: { inputs: {
// prompt: "make text red", prompt: "use better icons",
// }, },
// }, },
// ]; "2": {
hash: "2",
dateCreated: new Date(),
isCommitted: false,
type: "ai_edit",
parentHash: "1",
variants: [{ code: "<html>3. edit with better icons and red text</html>" }],
selectedVariantIndex: 0,
inputs: {
prompt: "make text red",
},
},
};
// const basicLinearHistoryWithCode: History = [ const basicLinearHistoryWithCode: Record<CommitHash, Commit> = {
// { "0": {
// type: "code_create", hash: "0",
// parentIndex: null, dateCreated: new Date(),
// code: "<html>1. create</html>", isCommitted: false,
// inputs: { type: "code_create",
// code: "<html>1. create</html>", parentHash: null,
// }, variants: [{ code: "<html>1. create</html>" }],
// }, selectedVariantIndex: 0,
// ...basicLinearHistory.slice(1), inputs: null,
// ]; },
...Object.fromEntries(Object.entries(basicLinearHistory).slice(1)),
};
// const basicBranchingHistory: History = [ const basicBranchingHistory: Record<CommitHash, Commit> = {
// ...basicLinearHistory, ...basicLinearHistory,
// { "3": {
// type: "ai_edit", hash: "3",
// parentIndex: 1, dateCreated: new Date(),
// code: "<html>4. edit with better icons and green text</html>", isCommitted: false,
// inputs: { type: "ai_edit",
// prompt: "make text green", parentHash: "1",
// }, variants: [
// }, { code: "<html>4. edit with better icons and green text</html>" },
// ]; ],
selectedVariantIndex: 0,
inputs: {
prompt: "make text green",
},
},
};
// const longerBranchingHistory: History = [ const longerBranchingHistory: Record<CommitHash, Commit> = {
// ...basicBranchingHistory, ...basicBranchingHistory,
// { "4": {
// type: "ai_edit", hash: "4",
// parentIndex: 3, dateCreated: new Date(),
// code: "<html>5. edit with better icons and green, bold text</html>", isCommitted: false,
// inputs: { type: "ai_edit",
// prompt: "make text bold", parentHash: "3",
// }, variants: [
// }, { code: "<html>5. edit with better icons and green, bold text</html>" },
// ]; ],
selectedVariantIndex: 0,
inputs: {
prompt: "make text bold",
},
},
};
// const basicBadHistory: History = [ const basicBadHistory: Record<CommitHash, Commit> = {
// { "0": {
// type: "ai_create", hash: "0",
// parentIndex: null, dateCreated: new Date(),
// code: "<html>1. create</html>", isCommitted: false,
// inputs: { type: "ai_create",
// image_url: "", parentHash: null,
// }, variants: [{ code: "<html>1. create</html>" }],
// }, selectedVariantIndex: 0,
// { inputs: {
// type: "ai_edit", image_url: "",
// parentIndex: 2, // <- Bad parent index },
// code: "<html>2. edit with better icons</html>", },
// inputs: { "1": {
// prompt: "use better icons", hash: "1",
// }, dateCreated: new Date(),
// }, isCommitted: false,
// ]; type: "ai_edit",
parentHash: "2", // <- Bad parent hash
variants: [{ code: "<html>2. edit with better icons</html>" }],
selectedVariantIndex: 0,
inputs: {
prompt: "use better icons",
},
},
};
// describe("History Utils", () => { describe("History Utils", () => {
// test("should correctly extract the history tree", () => { test("should correctly extract the history tree", () => {
// expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([ expect(extractHistory("2", basicLinearHistory)).toEqual([
// "<html>1. create</html>", "<html>1. create</html>",
// "use better icons", "use better icons",
// "<html>2. edit with better icons</html>", "<html>2. edit with better icons</html>",
// "make text red", "make text red",
// "<html>3. edit with better icons and red text</html>", "<html>3. edit with better icons and red text</html>",
// ]); ]);
// expect(extractHistoryTree(basicLinearHistory, 0)).toEqual([ expect(extractHistory("0", basicLinearHistory)).toEqual([
// "<html>1. create</html>", "<html>1. create</html>",
// ]); ]);
// // Test branching // Test branching
// expect(extractHistoryTree(basicBranchingHistory, 3)).toEqual([ expect(extractHistory("3", basicBranchingHistory)).toEqual([
// "<html>1. create</html>", "<html>1. create</html>",
// "use better icons", "use better icons",
// "<html>2. edit with better icons</html>", "<html>2. edit with better icons</html>",
// "make text green", "make text green",
// "<html>4. edit with better icons and green text</html>", "<html>4. edit with better icons and green text</html>",
// ]); ]);
// expect(extractHistoryTree(longerBranchingHistory, 4)).toEqual([ expect(extractHistory("4", longerBranchingHistory)).toEqual([
// "<html>1. create</html>", "<html>1. create</html>",
// "use better icons", "use better icons",
// "<html>2. edit with better icons</html>", "<html>2. edit with better icons</html>",
// "make text green", "make text green",
// "<html>4. edit with better icons and green text</html>", "<html>4. edit with better icons and green text</html>",
// "make text bold", "make text bold",
// "<html>5. edit with better icons and green, bold text</html>", "<html>5. edit with better icons and green, bold text</html>",
// ]); ]);
// expect(extractHistoryTree(longerBranchingHistory, 2)).toEqual([ expect(extractHistory("2", longerBranchingHistory)).toEqual([
// "<html>1. create</html>", "<html>1. create</html>",
// "use better icons", "use better icons",
// "<html>2. edit with better icons</html>", "<html>2. edit with better icons</html>",
// "make text red", "make text red",
// "<html>3. edit with better icons and red text</html>", "<html>3. edit with better icons and red text</html>",
// ]); ]);
// // Errors // Errors
// // Bad index // Bad hash
// expect(() => extractHistoryTree(basicLinearHistory, 100)).toThrow(); expect(() => extractHistory("100", basicLinearHistory)).toThrow();
// expect(() => extractHistoryTree(basicLinearHistory, -2)).toThrow();
// // Bad tree // Bad tree
// expect(() => extractHistoryTree(basicBadHistory, 1)).toThrow(); expect(() => extractHistory("1", basicBadHistory)).toThrow();
// }); });
// test("should correctly render the history tree", () => { test("should correctly render the history tree", () => {
// expect(renderHistory(basicLinearHistory, 2)).toEqual([ expect(renderHistory(Object.values(basicLinearHistory))).toEqual([
// { {
// isActive: false, ...basicLinearHistory["0"],
// parentVersion: null, type: "Create",
// summary: "Create", summary: "Create",
// type: "Create", parentVersion: null,
// }, },
// { {
// isActive: false, ...basicLinearHistory["1"],
// parentVersion: null, type: "Edit",
// summary: "use better icons", summary: "use better icons",
// type: "Edit", parentVersion: null,
// }, },
// { {
// isActive: true, ...basicLinearHistory["2"],
// parentVersion: null, type: "Edit",
// summary: "make text red", summary: "make text red",
// type: "Edit", parentVersion: null,
// }, },
// ]); ]);
// // Current version is the first version // Render a history with code
// expect(renderHistory(basicLinearHistory, 0)).toEqual([ expect(renderHistory(Object.values(basicLinearHistoryWithCode))).toEqual([
// { {
// isActive: true, ...basicLinearHistoryWithCode["0"],
// parentVersion: null, type: "Imported from code",
// summary: "Create", summary: "Imported from code",
// type: "Create", parentVersion: null,
// }, },
// { {
// isActive: false, ...basicLinearHistoryWithCode["1"],
// parentVersion: null, type: "Edit",
// summary: "use better icons", summary: "use better icons",
// type: "Edit", parentVersion: null,
// }, },
// { {
// isActive: false, ...basicLinearHistoryWithCode["2"],
// parentVersion: null, type: "Edit",
// summary: "make text red", summary: "make text red",
// type: "Edit", parentVersion: null,
// }, },
// ]); ]);
// // Render a history with code // Render a non-linear history
// expect(renderHistory(basicLinearHistoryWithCode, 0)).toEqual([ expect(renderHistory(Object.values(basicBranchingHistory))).toEqual([
// { {
// isActive: true, ...basicBranchingHistory["0"],
// parentVersion: null, type: "Create",
// summary: "Imported from code", summary: "Create",
// type: "Imported from code", parentVersion: null,
// }, },
// { {
// isActive: false, ...basicBranchingHistory["1"],
// parentVersion: null, type: "Edit",
// summary: "use better icons", summary: "use better icons",
// type: "Edit", parentVersion: null,
// }, },
// { {
// isActive: false, ...basicBranchingHistory["2"],
// parentVersion: null, type: "Edit",
// summary: "make text red", summary: "make text red",
// type: "Edit", parentVersion: null,
// }, },
// ]); {
...basicBranchingHistory["3"],
// // Render a non-linear history type: "Edit",
// expect(renderHistory(basicBranchingHistory, 3)).toEqual([ summary: "make text green",
// { parentVersion: 2,
// 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",
// },
// ]);
// });
// });