fix(app): clear promoted prompt origin
This commit is contained in:
parent
2b611a5b14
commit
b6113b68a0
2 changed files with 67 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ export function createPromptSubmissionState(input: {
|
|||
prompt: Prompt
|
||||
context: (ContextItem & { key: string })[]
|
||||
}) {
|
||||
const origin = input.target
|
||||
let target = input.target
|
||||
let cleared: Prompt | undefined
|
||||
|
||||
|
|
@ -15,6 +16,10 @@ export function createPromptSubmissionState(input: {
|
|||
context: input.context,
|
||||
target: () => target,
|
||||
clear() {
|
||||
if (origin !== target && origin.current() === input.prompt) {
|
||||
origin.reset()
|
||||
input.context.forEach((item) => origin.context.remove(item.key))
|
||||
}
|
||||
target.reset()
|
||||
cleared = target.current()
|
||||
},
|
||||
|
|
|
|||
|
|
@ -38,6 +38,68 @@ describe("prompt submission state", () => {
|
|||
expect(session.context.items()[0]).toMatchObject({ type: "file", path: "src/index.ts" })
|
||||
})
|
||||
|
||||
test("clears the original draft after first-submit promotion", () => {
|
||||
const draft = createPromptState()
|
||||
const session = createPromptState()
|
||||
draft.set([{ type: "text", content: "first prompt", start: 0, end: 12 }])
|
||||
draft.context.add({ type: "file", path: "src/index.ts" })
|
||||
const submission = createPromptSubmissionState({
|
||||
target: draft,
|
||||
prompt: draft.current(),
|
||||
context: draft.context.items().slice(),
|
||||
})
|
||||
|
||||
submission.retarget(session)
|
||||
submission.clear()
|
||||
|
||||
expect(draft.current()).toEqual([{ type: "text", content: "", start: 0, end: 0 }])
|
||||
expect(draft.context.items()).toEqual([])
|
||||
expect(session.current()).toEqual([{ type: "text", content: "", start: 0, end: 0 }])
|
||||
expect(session.context.items()).toHaveLength(1)
|
||||
expect(session.context.items()[0]).toMatchObject({ type: "file", path: "src/index.ts" })
|
||||
})
|
||||
|
||||
test("restores prompt and context to the promoted session after a cleared submit fails", () => {
|
||||
const draft = createPromptState()
|
||||
const session = createPromptState()
|
||||
draft.set([{ type: "text", content: "first prompt", start: 0, end: 12 }])
|
||||
draft.context.add({ type: "file", path: "src/index.ts" })
|
||||
const submission = createPromptSubmissionState({
|
||||
target: draft,
|
||||
prompt: draft.current(),
|
||||
context: draft.context.items().slice(),
|
||||
})
|
||||
|
||||
submission.retarget(session)
|
||||
submission.clear()
|
||||
const restored = submission.restore()
|
||||
restored?.target.set(restored.prompt)
|
||||
|
||||
expect(draft.current()).toEqual([{ type: "text", content: "", start: 0, end: 0 }])
|
||||
expect(draft.context.items()).toEqual([])
|
||||
expect(session.current()).toEqual([{ type: "text", content: "first prompt", start: 0, end: 12 }])
|
||||
expect(session.context.items()).toHaveLength(1)
|
||||
expect(session.context.items()[0]).toMatchObject({ type: "file", path: "src/index.ts" })
|
||||
})
|
||||
|
||||
test("does not clear an original draft edited after submission started", () => {
|
||||
const draft = createPromptState()
|
||||
const session = createPromptState()
|
||||
draft.set([{ type: "text", content: "submitted", start: 0, end: 9 }])
|
||||
const submission = createPromptSubmissionState({
|
||||
target: draft,
|
||||
prompt: draft.current(),
|
||||
context: [],
|
||||
})
|
||||
|
||||
submission.retarget(session)
|
||||
draft.set([{ type: "text", content: "new draft", start: 0, end: 9 }])
|
||||
submission.clear()
|
||||
|
||||
expect(draft.current()).toEqual([{ type: "text", content: "new draft", start: 0, end: 9 }])
|
||||
expect(session.current()).toEqual([{ type: "text", content: "", start: 0, end: 0 }])
|
||||
})
|
||||
|
||||
test("does not restore over a prompt edited after submission", () => {
|
||||
const target = createPromptState()
|
||||
target.set([{ type: "text", content: "submitted", start: 0, end: 9 }])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue