-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refresh AI cache on proposal update #254
base: main
Are you sure you want to change the base?
Changes from all commits
2e692bc
0559c22
9d44738
598905f
e05eae8
59857d0
eb670fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import { fetchProposal, Proposal } from '../../helpers/snapshot'; | |
import { IStorage } from '../storage/types'; | ||
import Cache from '../cache'; | ||
|
||
const tempCacheIds = new Map<string, number>(); | ||
|
||
class Summary extends Cache { | ||
proposal?: Proposal | null; | ||
openAi: OpenAI; | ||
|
@@ -16,9 +18,8 @@ class Summary extends Cache { | |
async isCacheable() { | ||
this.proposal = await fetchProposal(this.id); | ||
|
||
if (!this.proposal) { | ||
throw new Error('RECORD_NOT_FOUND'); | ||
} | ||
if (!this.proposal) throw new Error('RECORD_NOT_FOUND'); | ||
if (this.#cacheExpired()) return false; | ||
|
||
return true; | ||
} | ||
|
@@ -53,6 +54,18 @@ class Summary extends Cache { | |
throw e.error?.code ? new Error(e.error?.code.toUpperCase()) : e; | ||
} | ||
}; | ||
|
||
#cacheExpired = () => { | ||
const { id, state, updated } = this.proposal!; | ||
|
||
if (state !== 'pending') return false; | ||
|
||
return tempCacheIds.has(id) && tempCacheIds.get(id) !== updated; | ||
}; | ||
|
||
afterCreateCache() { | ||
tempCacheIds.set(this.proposal!.id, this.proposal!.updated); | ||
} | ||
Comment on lines
+63
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can't relay on updated param for cache, a pending proposal can be updated any number of times There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh updated is timestamp. got it |
||
} | ||
|
||
export default Summary; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Summary from '../../../../src/lib/ai/summary'; | ||
import * as snapshotHelper from '../../../../src/helpers/snapshot'; | ||
import { storageEngine } from '../../../../src/helpers/utils'; | ||
|
||
const fetchProposalMock = jest.spyOn(snapshotHelper, 'fetchProposal'); | ||
|
||
describe('AI summary', () => { | ||
describe('isCacheable()', () => { | ||
describe('when the proposal is pending', () => { | ||
it('returns true if the proposal has not been cached yet', () => { | ||
expect.assertions(2); | ||
const summary = new Summary('1', storageEngine()); | ||
fetchProposalMock.mockResolvedValueOnce({ | ||
id: '2', | ||
state: 'pending', | ||
updated: 1 | ||
} as snapshotHelper.Proposal); | ||
|
||
expect(summary.isCacheable()).resolves.toBe(true); | ||
expect(fetchProposalMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('returns true if the proposal has not been updated since last cache', async () => { | ||
expect.assertions(2); | ||
const summary = new Summary('summary-1', storageEngine()); | ||
fetchProposalMock.mockResolvedValueOnce({ | ||
id: '1', | ||
state: 'pending', | ||
updated: 1 | ||
} as snapshotHelper.Proposal); | ||
await summary.isCacheable(); | ||
summary.afterCreateCache(); | ||
|
||
fetchProposalMock.mockResolvedValueOnce({ | ||
id: '1', | ||
state: 'pending', | ||
updated: 2 | ||
} as snapshotHelper.Proposal); | ||
|
||
expect(summary.isCacheable()).resolves.toBe(false); | ||
expect(fetchProposalMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('returns false if the proposal has been updated since last cache', async () => { | ||
expect.assertions(2); | ||
const summary = new Summary('1', storageEngine()); | ||
fetchProposalMock.mockResolvedValue({ | ||
id: '3', | ||
state: 'pending', | ||
updated: 1 | ||
} as snapshotHelper.Proposal); | ||
await summary.isCacheable(); | ||
summary.afterCreateCache(); | ||
|
||
expect(summary.isCacheable()).resolves.toBe(true); | ||
expect(fetchProposalMock).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
|
||
it('returns true when the proposal exist', async () => { | ||
expect.assertions(2); | ||
const summary = new Summary('1', storageEngine()); | ||
fetchProposalMock.mockResolvedValueOnce({} as snapshotHelper.Proposal); | ||
|
||
expect(summary.isCacheable()).resolves.toBe(true); | ||
expect(fetchProposalMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('returns false when the proposal does not exist', () => { | ||
expect.assertions(2); | ||
const summary = new Summary('1', storageEngine()); | ||
fetchProposalMock.mockRejectedValueOnce(new Error('RECORD_NOT_FOUND')); | ||
|
||
expect(summary.isCacheable()).rejects.toThrow('RECORD_NOT_FOUND'); | ||
expect(fetchProposalMock).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If cache is expired,
isCacheable
should returntrue
right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
false
, since the item is not cache-able anymore