Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ describe("SavedWorkflowSectionComponent", () => {

describe("AI generate workflow (dashboard entry point)", () => {
const ipynbFile = { name: "analysis.ipynb" } as NzUploadFile;
// Migration errors stay up longer than the default toast so their instructions can be read.
const MIGRATION_ERROR_OPTIONS = { nzDuration: 8000 };
const AI_BUTTON_SELECTOR = 'button[title="AI generate a workflow from source code"]';

// Opens the modal and returns the requestImport callback the component handed to it; calling
Expand Down Expand Up @@ -419,7 +421,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()({ name: "data.txt" } as NzUploadFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Please upload a Jupyter Notebook (.ipynb) or a Python (.py) file.");
expect(errorSpy).toHaveBeenCalledWith(
"Please upload a Jupyter Notebook (.ipynb) or a Python (.py) file.",
MIGRATION_ERROR_OPTIONS
);
expect(notebookSpy).not.toHaveBeenCalled();
expect(scriptSpy).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -468,7 +473,8 @@ describe("SavedWorkflowSectionComponent", () => {

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses a python kernel, not R. Switch the notebook type to Python Notebook."
"This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook.",
MIGRATION_ERROR_OPTIONS
);
expect(migration.sendToAIGenerateWorkflow).not.toHaveBeenCalled();
});
Expand All @@ -481,12 +487,210 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()({ name: "analysis.py" } as NzUploadFile, "gpt-4", "r");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file.");
expect(errorSpy).toHaveBeenCalledWith(
"This is a Python file. Upload it under the Python File tab.",
MIGRATION_ERROR_OPTIONS
);
expect(migration.parseAndTagNotebook).not.toHaveBeenCalled();
expect(scriptSpy).not.toHaveBeenCalled();
});
});

describe("language mismatches", () => {
function spies(notebook: unknown = { cells: [] }) {
const migration = TestBed.inject(NotebookMigrationService);
return {
notebookConvert: vi.spyOn(migration, "sendToAIGenerateWorkflow"),
notebookParse: vi.spyOn(migration, "parseAndTagNotebook").mockResolvedValue(notebook as any),
scriptParse: vi.spyOn(migration, "parseScriptFile"),
folderParse: vi.spyOn(migration, "parseFolder").mockRejectedValue(new Error("stop")),
errorSpy: vi.spyOn(TestBed.inject(NotificationService), "error").mockImplementation(() => {}),
};
}

function enableR() {
(TestBed.inject(GuiConfigService) as unknown as MockGuiConfigService).setConfig({
rNotebookMigrationEnabled: true,
});
}

function pickedFile(relativePath: string): NzUploadFile {
const file = new File([""], relativePath.split("/").pop() ?? relativePath);
Object.defineProperty(file, "webkitRelativePath", { value: relativePath });
return file as unknown as NzUploadFile;
}

const rKernel = { cells: [], metadata: { kernelspec: { language: "R" } } };

it("points an R notebook uploaded as Python at the R notebook type", async () => {
enableR();
const { notebookConvert, errorSpy } = spies(rKernel);

const proceed = await getRequestImport()(ipynbFile, "gpt-4", "python");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook.",
MIGRATION_ERROR_OPTIONS
);
expect(notebookConvert).not.toHaveBeenCalled();
});

it("says R is off instead of pointing at a disabled control", async () => {
const { notebookConvert, errorSpy } = spies(rKernel);

await getRequestImport()(ipynbFile, "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses an R kernel, and R is not enabled on this deployment.",
MIGRATION_ERROR_OPTIONS
);
expect(notebookConvert).not.toHaveBeenCalled();
});

it("ignores case and surrounding space in the declared language", async () => {
const { errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: " PYTHON " } } });

await getRequestImport()(ipynbFile, "gpt-4", "r");

expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses a Python kernel, not R. Switch the notebook type to Python Notebook.",
MIGRATION_ERROR_OPTIONS
);
});

it("falls back to language_info when there is no kernelspec", async () => {
enableR();
const { errorSpy } = spies({ cells: [], metadata: { language_info: { name: "R" } } });

await getRequestImport()(ipynbFile, "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses an R kernel, not Python. Switch the notebook type to R Notebook.",
MIGRATION_ERROR_OPTIONS
);
});

it("rejects a kernel in no supported language whichever type was chosen", async () => {
const { notebookConvert, errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: "julia" } } });

await getRequestImport()(ipynbFile, "gpt-4", "python");
await getRequestImport()(ipynbFile, "gpt-4", "r");

const message = "This notebook uses a julia kernel. Only Python and R notebooks can be converted.";
expect(errorSpy.mock.calls).toEqual([
[message, MIGRATION_ERROR_OPTIONS],
[message, MIGRATION_ERROR_OPTIONS],
]);
expect(notebookConvert).not.toHaveBeenCalled();
});

it("uses an before a kernel language starting with a vowel", async () => {
const { errorSpy } = spies({ cells: [], metadata: { kernelspec: { language: "ocaml" } } });

await getRequestImport()(ipynbFile, "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This notebook uses an ocaml kernel. Only Python and R notebooks can be converted.",
MIGRATION_ERROR_OPTIONS
);
});

it("points an R file uploaded under a Python tab at the R File tab", async () => {
enableR();
const { scriptParse, errorSpy } = spies();

const proceed = await getRequestImport()({ name: "analysis.R" } as NzUploadFile, "gpt-4", "python");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"This is an R file. Upload it under the R File tab.",
MIGRATION_ERROR_OPTIONS
);
expect(scriptParse).not.toHaveBeenCalled();
});

it("says R is off for an R file while R is disabled", async () => {
const { errorSpy } = spies();

await getRequestImport()({ name: "analysis.r" } as NzUploadFile, "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This is an R file, and R is not enabled on this deployment.",
MIGRATION_ERROR_OPTIONS
);
});

it("keeps the upload hint for an extension no language claims", async () => {
const { errorSpy } = spies();

await getRequestImport()({ name: "data.txt" } as NzUploadFile, "gpt-4", "r");

expect(errorSpy).toHaveBeenCalledWith(
"Please upload an R Jupyter Notebook (.ipynb) or an R (.R) file.",
MIGRATION_ERROR_OPTIONS
);
});

it("points a folder of Python files on the R Folder tab at the Python Folder tab", async () => {
const { folderParse, errorSpy } = spies();

const proceed = await getRequestImport()(
[pickedFile("proj/main.py"), pickedFile("proj/README.md")],
"gpt-4",
"r"
);

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"This folder holds Python files, not R. Upload it under the Python Folder tab.",
MIGRATION_ERROR_OPTIONS
);
expect(folderParse).not.toHaveBeenCalled();
});

it("points a folder of R files on the Python Folder tab at the R Folder tab", async () => {
enableR();
const { folderParse, errorSpy } = spies();

await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This folder holds R files, not Python. Upload it under the R Folder tab.",
MIGRATION_ERROR_OPTIONS
);
expect(folderParse).not.toHaveBeenCalled();
});

it("says R is off for a folder of R files while R is disabled", async () => {
const { errorSpy } = spies();

await getRequestImport()([pickedFile("proj/main.R")], "gpt-4", "python");

expect(errorSpy).toHaveBeenCalledWith(
"This folder holds R files, and R is not enabled on this deployment.",
MIGRATION_ERROR_OPTIONS
);
});

it("leaves a folder with files in the selected language to parseFolder, even if it holds others too", async () => {
const { folderParse } = spies();

await getRequestImport()([pickedFile("proj/main.R"), pickedFile("proj/legacy.py")], "gpt-4", "r");

expect(folderParse).toHaveBeenCalled();
});

it("leaves a folder with no source in any language to parseFolder's own message", async () => {
const { folderParse, errorSpy } = spies();

await getRequestImport()([pickedFile("proj/README.md")], "gpt-4", "r");

expect(folderParse).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalledWith("stop", MIGRATION_ERROR_OPTIONS);
});
});

describe("R file input", () => {
function mockRScriptGenerationSuccess() {
const migration = TestBed.inject(NotebookMigrationService);
Expand All @@ -506,10 +710,11 @@ describe("SavedWorkflowSectionComponent", () => {

["analysis.R", "analysis.r"].forEach(name => {
it(`converts ${name} as an R script`, async () => {
const { convertSpy } = mockRScriptGenerationSuccess();
const { migration, convertSpy } = mockRScriptGenerationSuccess();

const proceed = await getRequestImport()({ name } as NzUploadFile, "gpt-4", "r");

expect(migration.parseScriptFile).toHaveBeenCalledWith(expect.anything(), "r");
expect(convertSpy).toHaveBeenCalledWith("x <- 1\n", "gpt-4", "r");
expect(proceed).toBe(true);
});
Expand All @@ -523,7 +728,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()({ name: "analysis.R" } as NzUploadFile, "gpt-4", "r");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Failed to read the R file. Please upload a valid, non-empty .R file.");
expect(errorSpy).toHaveBeenCalledWith(
"Failed to read the R file. Please upload a valid, non-empty .R file.",
MIGRATION_ERROR_OPTIONS
);
expect(convertSpy).not.toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -585,7 +793,8 @@ describe("SavedWorkflowSectionComponent", () => {

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"Failed to read the Python file. Please upload a valid, non-empty .py file."
"Failed to read the Python file. Please upload a valid, non-empty .py file.",
MIGRATION_ERROR_OPTIONS
);
expect(convertSpy).not.toHaveBeenCalled();
});
Expand All @@ -599,7 +808,7 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(pyFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the script"));
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the script"), MIGRATION_ERROR_OPTIONS);
});

it("reports a generation failure and resolves false", async () => {
Expand All @@ -611,7 +820,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(pyFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Error while communicating with the LLM, check console for details.");
expect(errorSpy).toHaveBeenCalledWith(
"Error while communicating with the LLM, check console for details.",
MIGRATION_ERROR_OPTIONS
);
});
});

Expand Down Expand Up @@ -673,7 +885,11 @@ describe("SavedWorkflowSectionComponent", () => {
const { parseSpy, convertSpy } = mockFolderGenerationSuccess();
vi.spyOn(TestBed.inject(Router), "navigate").mockResolvedValue(true);

const proceed = await getRequestImport()(selection, "gpt-4", "r");
const proceed = await getRequestImport()(
[pickedFile("proj/main.R"), pickedFile("proj/R/helpers.R")],
"gpt-4",
"r"
);

expect(parseSpy.mock.calls[0][1]).toBe("r");
expect(convertSpy).toHaveBeenCalledWith(assembled, "gpt-4", "r");
Expand Down Expand Up @@ -704,7 +920,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(selection, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("No Python files were found in the selected folder.");
expect(errorSpy).toHaveBeenCalledWith(
"No Python files were found in the selected folder.",
MIGRATION_ERROR_OPTIONS
);
expect(convertSpy).not.toHaveBeenCalled();
});

Expand All @@ -717,8 +936,8 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(selection, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("cut off"));
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("folder"));
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("cut off"), MIGRATION_ERROR_OPTIONS);
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("folder"), MIGRATION_ERROR_OPTIONS);
});

it("falls back to a generic message when something other than an Error is thrown", async () => {
Expand All @@ -729,7 +948,7 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(selection, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Failed to read the selected folder.");
expect(errorSpy).toHaveBeenCalledWith("Failed to read the selected folder.", MIGRATION_ERROR_OPTIONS);
});

it("names the folder in the timeout message so the advice matches the input", async () => {
Expand All @@ -741,7 +960,7 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(selection, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the folder"));
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("simplify the folder"), MIGRATION_ERROR_OPTIONS);
});

it("names the workflow after the folder when the file arrives wrapped", async () => {
Expand Down Expand Up @@ -779,7 +998,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(ipynbFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Failed to read the notebook file. Please upload a valid .ipynb file.");
expect(errorSpy).toHaveBeenCalledWith(
"Failed to read the notebook file. Please upload a valid .ipynb file.",
MIGRATION_ERROR_OPTIONS
);
expect(llmSpy).not.toHaveBeenCalled();
});

Expand All @@ -794,7 +1016,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(ipynbFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Error while communicating with the LLM, check console for details.");
expect(errorSpy).toHaveBeenCalledWith(
"Error while communicating with the LLM, check console for details.",
MIGRATION_ERROR_OPTIONS
);
expect(persist.createWorkflow).not.toHaveBeenCalled();
});

Expand All @@ -810,7 +1035,8 @@ describe("SavedWorkflowSectionComponent", () => {

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith(
"Generation timed out after 10 minutes. Try again, choose a faster model, or simplify the notebook."
"Generation timed out after 10 minutes. Try again, choose a faster model, or simplify the notebook.",
MIGRATION_ERROR_OPTIONS
);
expect(persist.createWorkflow).not.toHaveBeenCalled();
});
Expand All @@ -830,7 +1056,10 @@ describe("SavedWorkflowSectionComponent", () => {
const proceed = await getRequestImport()(ipynbFile, "gpt-4");

expect(proceed).toBe(false);
expect(errorSpy).toHaveBeenCalledWith("Failed to save the generated workflow, check console for details.");
expect(errorSpy).toHaveBeenCalledWith(
"Failed to save the generated workflow, check console for details.",
MIGRATION_ERROR_OPTIONS
);
expect(storeSpy).not.toHaveBeenCalled();
});

Expand Down
Loading
Loading