|
36 | 36 | // } |
37 | 37 | // } |
38 | 38 |
|
39 | | -import { Ollama } from '@langchain/ollama' |
40 | | -import { PromptTemplate } from '@langchain/core/prompts' |
41 | | -import { resolve } from 'path' |
42 | | -import { sanitize } from 'dompurify' |
| 39 | +import { PromptTemplate } from '@langchain/core/prompts'; |
| 40 | +import { Ollama } from '@langchain/ollama'; |
| 41 | +import { sanitize } from 'dompurify'; |
| 42 | +import { resolve } from 'path'; |
43 | 43 |
|
44 | 44 | const llm = new Ollama({ |
45 | 45 | model: 'qwen2.5-coder', |
46 | 46 | numCtx: 16384, // truncating input prompt limit=4096 |
47 | | -}) |
| 47 | +}); |
48 | 48 |
|
49 | 49 | const template = ` |
50 | 50 | You're a QA engineer writing an E2E test step with Cypress |
|
62 | 62 | \`\`\`html |
63 | 63 | {html} |
64 | 64 | \`\`\` |
65 | | -` |
| 65 | +`; |
66 | 66 |
|
67 | | -const prompt = PromptTemplate.fromTemplate(template.trim()) |
68 | | -const chain = prompt.pipe(llm) |
| 67 | +const prompt = PromptTemplate.fromTemplate(template.trim()); |
| 68 | +const chain = prompt.pipe(llm); |
69 | 69 |
|
70 | 70 | function minutesToMilliseconds(minutes: number) { |
71 | | - return 1000 * 60 * minutes |
| 71 | + return 1000 * 60 * minutes; |
72 | 72 | } |
73 | 73 |
|
74 | 74 | function getGeneratedFilePath(): string { |
75 | | - return resolve(Cypress.spec.absolute, `../__generated__/${Cypress.spec.name}.json`) |
| 75 | + return resolve( |
| 76 | + Cypress.spec.absolute, |
| 77 | + `../__generated__/${Cypress.spec.name}.json`, |
| 78 | + ); |
76 | 79 | } |
77 | 80 |
|
78 | | -const noop = () => {} |
| 81 | +const noop = () => {}; |
79 | 82 |
|
80 | 83 | function readGeneratedFile() { |
81 | | - return cy.readFile(getGeneratedFilePath(), { log: false }).should(noop) |
| 84 | + return cy.readFile(getGeneratedFilePath(), { log: false }).should(noop); |
82 | 85 | } |
83 | 86 |
|
84 | 87 | function getTestKey(): string { |
85 | | - return Cypress.currentTest.titlePath.join(' ') |
| 88 | + return Cypress.currentTest.titlePath.join(' '); |
86 | 89 | } |
87 | 90 |
|
88 | 91 | function saveGeneratedCode(task: string, code: string) { |
89 | 92 | readGeneratedFile().then((contents) => { |
90 | | - contents = contents || {} |
| 93 | + contents = contents || {}; |
91 | 94 |
|
92 | | - const key = getTestKey() |
93 | | - contents[key] = contents[key] || {} |
94 | | - contents[key][task] = code |
| 95 | + const key = getTestKey(); |
| 96 | + contents[key] = contents[key] || {}; |
| 97 | + contents[key][task] = code; |
95 | 98 |
|
96 | | - cy.writeFile(getGeneratedFilePath(), JSON.stringify(contents, null, 2), { log: false }) |
97 | | - }) |
| 99 | + cy.writeFile(getGeneratedFilePath(), JSON.stringify(contents, null, 2), { |
| 100 | + log: false, |
| 101 | + }); |
| 102 | + }); |
98 | 103 | } |
99 | 104 |
|
100 | | -function getGeneratedCode(task: string) { |
101 | | - return readGeneratedFile().then((json) => { |
102 | | - try { |
103 | | - return json[getTestKey()][task] |
104 | | - } catch (error) { |
105 | | - return '' |
106 | | - } |
107 | | - }) |
108 | | -} |
109 | | - |
110 | | -Cypress.Commands.add('ai', (task, options) => { |
111 | | - Cypress.log({ displayName: 'ai', message: task }) |
| 105 | +Cypress.Commands.add('ai', (task) => { |
| 106 | + Cypress.log({ displayName: 'ai', message: task }); |
112 | 107 |
|
113 | 108 | return readGeneratedFile().then((json) => { |
114 | 109 | try { |
115 | | - const code = json[getTestKey()][task] |
116 | | - |
117 | | - if (code) { |
118 | | - return eval(code) |
119 | | - } |
120 | | - } catch (error) { |
121 | | - // pass |
122 | | - } |
123 | | - |
124 | | - cy.document({ log: false }).then({ timeout: minutesToMilliseconds(2) }, async (doc) => { |
125 | | - const response = await chain.invoke({ |
126 | | - task, |
127 | | - // html: sanitize(doc.documentElement.outerHTML), |
128 | | - html: sanitize(doc.body.innerHTML), |
129 | | - }) |
130 | | - // console.log(response) |
131 | | - |
132 | | - const error = "I'm sorry, but I can't assist with that request." |
133 | | - if (response.includes(error)) { |
134 | | - throw new Error(error) |
135 | | - } |
| 110 | + const code = json[getTestKey()][task]; |
136 | 111 |
|
137 | | - const code = response.match(/```(javascript|js)?([\s\S]+?)```/)?.[2]?.trim() |
138 | 112 | if (code) { |
139 | | - eval(code) |
140 | | - saveGeneratedCode(task, code) |
| 113 | + return eval(code); |
141 | 114 | } |
142 | | - }) |
143 | | - }) |
144 | | -}) |
| 115 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-empty |
| 116 | + } catch (error) {} |
| 117 | + |
| 118 | + cy.document({ log: false }).then( |
| 119 | + { timeout: minutesToMilliseconds(2) }, |
| 120 | + async (doc) => { |
| 121 | + const response = await chain.invoke({ |
| 122 | + task, |
| 123 | + // html: sanitize(doc.documentElement.outerHTML), |
| 124 | + html: sanitize(doc.body.innerHTML), |
| 125 | + }); |
| 126 | + // console.log(response) |
| 127 | + |
| 128 | + const error = "I'm sorry, but I can't assist with that request."; |
| 129 | + if (response.includes(error)) { |
| 130 | + throw new Error(error); |
| 131 | + } |
| 132 | + |
| 133 | + const code = response |
| 134 | + .match(/```(javascript|js)?([\s\S]+?)```/)?.[2] |
| 135 | + ?.trim(); |
| 136 | + if (code) { |
| 137 | + eval(code); |
| 138 | + saveGeneratedCode(task, code); |
| 139 | + } |
| 140 | + }, |
| 141 | + ); |
| 142 | + }); |
| 143 | +}); |
145 | 144 |
|
146 | 145 | declare global { |
| 146 | + // eslint-disable-next-line @typescript-eslint/no-namespace |
147 | 147 | namespace Cypress { |
148 | 148 | interface Chainable { |
149 | | - ai(task: string, options?: object): Chainable<void> |
| 149 | + ai(task: string): Chainable<void>; |
150 | 150 | } |
151 | 151 | } |
152 | 152 | } |
153 | 153 |
|
154 | | -export {} |
| 154 | +export {}; |
0 commit comments