Never silently truncate the draft when attaching an IPFS link

Check before uploading that the link (from the gateway and a CIDv1-sized
placeholder) fits with the draft and the reply prefix, and tell the user
when it doesn't instead of cutting their text. The channel limit now
counts the @[name] reply prefix like the send path.
This commit is contained in:
zjs81 committed 2026-10-24 21:57:55 -07:00
1 parent 084a8fb98b
commit 34db64d7af
25 files changed
+315 -103

No files matched your search

+57 -62
View File
@@ -1,76 +1,71 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/ipfs_link_attach_flow.dart';
import 'package:meshcore_open/helpers/ipfs_upload_helper.dart';
void main() {
const url = 'ipfs://cid';
String identity(String text) => text;
test('puts the link before an empty draft with a trailing space', () {
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: '',
maxBytes: 100,
encodeOutbound: identity,
),
'$url ',
);
group('composeIpfsLinkDraft', () {
test('puts the link before an empty draft with a trailing space', () {
expect(composeIpfsLinkDraft(messageUrl: url, existingText: ''), '$url ');
});
test('keeps the whole draft after the link', () {
final draft = 'x' * 500;
expect(
composeIpfsLinkDraft(messageUrl: url, existingText: draft),
'$url\n$draft',
);
});
});
test('keeps a draft that fits after the link', () {
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: 'hello',
maxBytes: 100,
encodeOutbound: identity,
),
'$url\nhello',
);
group('ipfsLinkDraftFits', () {
test('accepts a draft at the limit and rejects one byte over', () {
expect(
ipfsLinkDraftFits('$url\nhi', maxBytes: 13, encodeOutbound: identity),
isTrue,
);
expect(
ipfsLinkDraftFits('$url\nhi', maxBytes: 12, encodeOutbound: identity),
isFalse,
);
});
test('measures the encoded form, including a reply prefix', () {
String withReply(String text) => '@[Alice] $text';
expect(
ipfsLinkDraftFits('$url\nhi', maxBytes: 13, encodeOutbound: withReply),
isFalse,
);
expect(
ipfsLinkDraftFits('$url\nhi', maxBytes: 22, encodeOutbound: withReply),
isTrue,
);
});
test('ignores the trailing space the send path trims', () {
expect(
ipfsLinkDraftFits(
'$url ',
maxBytes: url.length,
encodeOutbound: identity,
),
isTrue,
);
});
});
test('trims the draft until the encoded text fits', () {
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: 'hello',
maxBytes: url.length + 3,
encodeOutbound: identity,
),
'$url\nhe',
);
});
group('IpfsUploadHelper.linkForCid', () {
test('uses ipfs:// without a gateway', () {
expect(
IpfsUploadHelper.linkForCid(uploadGateway: '', cid: 'abc'),
'ipfs://abc',
);
});
test('measures the encoded form', () {
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: 'hello',
maxBytes: url.length + 3,
encodeOutbound: (text) => 'x$text',
),
'$url\nh',
);
});
test('returns null when the link alone does not fit', () {
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: 'hello',
maxBytes: 4,
encodeOutbound: identity,
),
isNull,
);
expect(
composeIpfsLinkDraft(
messageUrl: url,
existingText: '',
maxBytes: 4,
encodeOutbound: identity,
),
isNull,
);
test('sample CID is CIDv1 base32 length', () {
expect(IpfsUploadHelper.sampleCid.length, 59);
});
});
}