Skip to content
Merged
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
56 changes: 56 additions & 0 deletions core/src/duplicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,62 @@ describe('findApplicationMatch', () => {
});
});

describe('form apply-page addresses', () => {
const stored = 'https://jobs.lever.co/example/abc';
const data = snap({
jobs: [{ id: 'j1', createdAt: `2026-06-12${AT}`, canonicalUrl: stored }],
applications: [{ id: 'a1', createdAt: `2026-06-13${AT}`, jobId: 'j1' }],
});

it('findApplicationMatch matches the /apply address of a stored job', () => {
expect(
findApplicationMatch(data, { canonicalUrl: `${stored}/apply` }),
).toEqual({ kind: 'already_applied', applicationId: 'a1' });
expect(
findApplicationMatch(data, {
canonicalUrl: 'https://jobs.lever.co/example/other/apply',
}),
).toBeUndefined();
});

it('matches the Ashby /application address of a stored job', () => {
const ashby = 'https://jobs.ashbyhq.com/example/abc';
const ashbyData = snap({
jobs: [{ id: 'j2', createdAt: `2026-06-12${AT}`, canonicalUrl: ashby }],
applications: [{ id: 'a2', createdAt: `2026-06-13${AT}`, jobId: 'j2' }],
});
expect(
findApplicationMatch(ashbyData, { canonicalUrl: `${ashby}/application` }),
).toEqual({ kind: 'already_applied', applicationId: 'a2' });
expect(
repostFact(
ashbyData,
{ canonicalUrl: `${ashby}/application` },
'2026-11-15',
)?.firstSavedOn,
).toBe('2026-06-12');
expect(
findApplicationMatch(ashbyData, {
canonicalUrl: 'https://jobs.ashbyhq.com/example/zzz/application',
}),
).toBeUndefined();
});

it('repostFact matches the /apply address of a stored job', () => {
expect(
repostFact(data, { canonicalUrl: `${stored}/apply` }, '2026-11-15')
?.firstSavedOn,
).toBe('2026-06-12');
expect(
repostFact(
data,
{ canonicalUrl: 'https://jobs.lever.co/example/other/apply' },
'2026-11-15',
),
).toBeUndefined();
});
});

describe('repostFact', () => {
const job = (id: string, day: string, extra: Partial<Job> = {}): Job => ({
id,
Expand Down
13 changes: 11 additions & 2 deletions core/src/duplicates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { normalizeName } from './application-ops';
import { isLocalDate, toLocalDate } from './dates';
import { formUrlKey } from './form-link';
import type { Application, Id, Job, LocalDate, Snapshot } from './types';
import { canonicalizeUrl } from './url';

Expand Down Expand Up @@ -45,6 +46,14 @@ function safeCanonical(url: string): string | undefined {
}
}

// Equal, or both the same posting once a form's apply segment is dropped.
function urlsMatch(have: string | undefined, want: string): boolean {
if (have === undefined) return false;
if (have === want) return true;
const key = formUrlKey(want);
return key !== undefined && key === formUrlKey(have);
}

function applicationCanonicalUrl(
app: Application,
jobsById: Map<Id, Job>,
Expand Down Expand Up @@ -110,7 +119,7 @@ export function findApplicationMatch(
const appTitle = nameKey(app.title);
if (
wantUrl !== undefined &&
url === wantUrl &&
urlsMatch(url, wantUrl) &&
!contradicts(wantCompany, company, wantTitle, appTitle)
) {
if (
Expand Down Expand Up @@ -169,7 +178,7 @@ export function repostFact(
const jobTitle = nameKey(job.title);
const sameUrl =
wantUrl !== undefined &&
str(job.canonicalUrl) === wantUrl &&
urlsMatch(str(job.canonicalUrl), wantUrl) &&
!contradicts(wantCompany, jobCompany, wantTitle, jobTitle);
const sameName =
wantCompany !== undefined &&
Expand Down
43 changes: 43 additions & 0 deletions core/src/form-application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,49 @@ function stores(ops: WriteOp[], store: string): WriteOp[] {
return ops.filter((o) => o.kind === 'put' && o.store === store);
}

describe('newApplicationFromFormOps facts', () => {
const archivedJob: Job = {
id: 'oldjob',
source: 'paste',
title: 'Dev',
companyName: 'Acme',
canonicalUrl: 'https://jobs.lever.co/acme/1',
createdAt: '2025-12-01T00:00:00.000Z',
};

it('reports a duplicate and a repost of an earlier job at the form key', () => {
const data: Snapshot = {
...emptySnapshot(),
jobs: [archivedJob],
applications: [
{
id: 'oldapp',
jobId: 'oldjob',
title: 'Dev',
createdAt: '2025-12-01T00:00:00.000Z',
},
],
};
const built = newApplicationFromFormOps(
data,
{ source: 'paste', url: URL, title: 'Dev', companyName: 'Acme' },
makeDeps(),
);
expect(built.match).toBe('already_applied');
expect(built.repost).toMatch(/^You saved this posting on /);
});

it('reports nothing for a new posting and never counts itself', () => {
const built = newApplicationFromFormOps(
emptySnapshot(),
{ source: 'paste', url: URL, title: 'Dev', companyName: 'Acme' },
makeDeps(),
);
expect(built.match).toBeUndefined();
expect(built.repost).toBeUndefined();
});
});

describe('newApplicationFromFormOps', () => {
it('makes one job and one linked interested application', () => {
const { ops, applicationId, record } = newApplicationFromFormOps(
Expand Down
38 changes: 35 additions & 3 deletions core/src/form-application.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { toLocalDate } from './dates';
import {
findApplicationMatch,
repostFact,
type ApplicationMatch,
type JobCandidate,
} from './duplicates';
import type { FillResult } from './fill/result';
import { fillRecordOps, type FillRecord } from './fill-records';
import { formUrlKey, linkFormOps } from './form-link';
Expand Down Expand Up @@ -48,7 +55,13 @@ export function newApplicationFromFormOps(
input: JobInput,
deps: Deps,
held?: FillResult,
): { ops: WriteOp[]; applicationId: Id; record?: FillRecord } {
): {
ops: WriteOp[];
applicationId: Id;
record?: FillRecord;
match?: ApplicationMatch['kind'];
repost?: string;
} {
const saved = saveJobOps(input, deps);
const jobPut = saved.ops[0];
if (jobPut?.kind !== 'put' || jobPut.store !== 'jobs') {
Expand All @@ -68,7 +81,26 @@ export function newApplicationFromFormOps(
const ops: WriteOp[] = [...created.ops];
ops.splice(companyIndex + 1, 0, jobOp);

if (held === undefined) return { ops, applicationId: created.applicationId };
// Facts only inform, as in Save job: computed on the data before this write,
// for a candidate stored the way the job is (form key as its canonical URL).
const candidate: JobCandidate = {};
if (job.canonicalUrl !== undefined) candidate.canonicalUrl = job.canonicalUrl;
if (job.companyName !== undefined) candidate.companyName = job.companyName;
if (job.title !== undefined) candidate.title = job.title;
const match = findApplicationMatch(data, candidate)?.kind;
const repost = repostFact(
data,
{ ...candidate, excludeJobId: job.id },
toLocalDate(new Date(deps.now())),
)?.text;
const facts = {
...(match !== undefined ? { match } : {}),
...(repost !== undefined ? { repost } : {}),
};

if (held === undefined) {
return { ops, applicationId: created.applicationId, ...facts };
}

const next: Snapshot = {
...data,
Expand All @@ -88,5 +120,5 @@ export function newApplicationFromFormOps(
}
const record = fillRecordOps(next, held, created.applicationId, deps);
ops.push(...record.ops);
return { ops, applicationId: created.applicationId, record };
return { ops, applicationId: created.applicationId, record, ...facts };
}
9 changes: 9 additions & 0 deletions core/src/form-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ function app(id: string, day: string, extra: object = {}): Application {
const KEY = 'https://example.com/jobs/1';

describe('formUrlKey', () => {
it('ignores an address longer than the capture cap', () => {
const long = `https://example.com/${'a'.repeat(8_001 - 20)}`;
expect(long).toHaveLength(8_001);
expect(formUrlKey(long)).toBeUndefined();
expect(formUrlKey('https://example.com/jobs/1')).toBe(
'https://example.com/jobs/1',
);
});

it('strips the Lever apply segment', () => {
expect(formUrlKey('https://jobs.lever.co/acme/123/apply')).toBe(
formUrlKey('https://jobs.lever.co/acme/123'),
Expand Down
3 changes: 3 additions & 0 deletions core/src/form-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface UrlLike {
declare const URL: { new (url: string, base?: string): UrlLike };

export const MAX_FORM_URLS = 20;
// Matches the capture URL cap (shell/capture MAX_CAPTURE_URL); core cannot import it.
export const MAX_FORM_URL = 8_000;

// Hosts whose application form lives one path segment below the posting.
const TRAILING_SEGMENTS: Readonly<Record<string, string>> = {
Expand All @@ -24,6 +26,7 @@ export function formUrlKey(
url: string,
relCanonical?: string,
): string | undefined {
if (url.length > MAX_FORM_URL) return undefined;
try {
const parsed = new URL(url.trim());
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
Expand Down
50 changes: 47 additions & 3 deletions shell/ui/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ export function Popup(): VNode {
const [recordNotice, setRecordNotice] = useState<string | undefined>(
undefined,
);
// Duplicate and repost facts for the application just made from this form.
const [newFacts, setNewFacts] = useState<
{ match: Facts['match']; repost: string | undefined } | undefined
>(undefined);
const linkingRef = useRef(false);
const keepHeldRef = useRef(false);
const linkedIdRef = useRef<Id | undefined>(undefined);

useEffect(() => {
Expand All @@ -165,11 +170,15 @@ export function Popup(): VNode {
// A match linked the form without the owner's click: drop the stale held
// fill rather than let a later Choose… record it. The owner's own link
// path records and clears held itself, and is excluded while in flight.
// Exception: onFilled holds a result on two recoverable paths (application
// gone, write rejected) where the popup still offers to record it; those
// keep it until the owner's next link (runLink resets this).
useEffect(() => {
if (
linkedIdRef.current !== undefined &&
heldRef.current !== undefined &&
!linkingRef.current
!linkingRef.current &&
!keepHeldRef.current
) {
heldRef.current = undefined;
setHeld(undefined);
Expand Down Expand Up @@ -289,6 +298,8 @@ export function Popup(): VNode {
id: Id;
name: string;
record?: FillRecord;
match?: Facts['match'];
repost?: string;
},
): Promise<boolean> => {
if (linkingRef.current) return Promise.resolve(false);
Expand All @@ -300,6 +311,8 @@ export function Popup(): VNode {
name: string;
record?: FillRecord;
held?: FillResult;
match?: Facts['match'];
repost?: string;
} = { name: '' };
return app
.apply((d) => {
Expand All @@ -309,21 +322,31 @@ export function Popup(): VNode {
got.id = built.id;
got.name = built.name;
got.record = built.record;
got.match = built.match;
got.repost = built.repost;
if (built.record?.missing) throw new Error(MISSING);
return built.ops;
})
.then(
() => {
linkingRef.current = false;
// Only a successful owner link ends the keep; a failed one leaves
// the held result offered for a retry.
keepHeldRef.current = false;
setChosenId(got.id);
setLinking(false);
setRecordNotice(undefined);
setNewFacts(
got.match !== undefined || got.repost !== undefined
? { match: got.match, repost: got.repost }
: undefined,
);
if (got.record !== undefined) {
// Clear only the result this link recorded; a Fill that finished
// during the write has replaced it and must survive.
if (got.held !== undefined && heldRef.current === got.held) {
setHeld(undefined);
}
setRecordNotice(undefined);
setRecordLine(
got.record.recorded > 0
? {
Expand Down Expand Up @@ -367,6 +390,7 @@ export function Popup(): VNode {
// Read when the fill completes, not at the click (FillSection is fixed).
const id = linkedIdRef.current;
if (id === undefined) {
keepHeldRef.current = false;
setHeld(result);
return;
}
Expand All @@ -383,12 +407,14 @@ export function Popup(): VNode {
const record = got.record;
if (record === undefined) return;
if (record.missing) {
keepHeldRef.current = true;
heldRef.current = result;
setHeld(result);
setChosenId(undefined);
setRecordNotice(MISSING);
return;
}
keepHeldRef.current = false;
setHeld(undefined);
if (record.recorded > 0) {
setRecordLine({
Expand All @@ -398,7 +424,12 @@ export function Popup(): VNode {
});
}
},
(err: unknown) => setLinkError(messageOf(err)),
(err: unknown) => {
keepHeldRef.current = true;
heldRef.current = result;
setHeld(result);
setLinkError(messageOf(err));
},
);
};
const linkNew = (names: {
Expand All @@ -425,6 +456,8 @@ export function Popup(): VNode {
id: built.applicationId,
name: displayName(names.companyName, names.title),
record: built.record,
match: built.match,
repost: built.repost,
};
});

Expand Down Expand Up @@ -604,6 +637,17 @@ export function Popup(): VNode {
recordLine={recordLine}
/>
) : null}
{newFacts?.match === 'already_applied' ? (
<Fact tone="neutral">Already applied here.</Fact>
) : null}
{newFacts?.match === 'possible_match' ? (
<Fact tone="neutral">
Possible match: an application for the same company and title.
</Fact>
) : null}
{newFacts?.repost ? (
<Fact tone="neutral">{newFacts.repost}</Fact>
) : null}
<FillSection controller={controller} onFilled={onFilled} />
{fillStatus === 'ready' && linkedId !== undefined ? (
<ToldPanel
Expand Down